第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

BMI 計(jì)算器,包含姓名、體重、身高、BMI 和使用數(shù)組和循環(huán)的文本

BMI 計(jì)算器,包含姓名、體重、身高、BMI 和使用數(shù)組和循環(huán)的文本

翻過高山走不出你 2022-12-28 10:46:27
我想問一下,當(dāng)循環(huán)開始并重新開始時(shí),字符串變量名稱將增加 1。這個(gè)程序應(yīng)該問你要寫多少患者。如果你為前任寫作。10,然后循環(huán)將進(jìn)行 10 次,它會(huì)詢問我想要的所有這些信息,然后將它們添加到我已經(jīng)創(chuàng)建的名為 BMI 的數(shù)組中。整個(gè)程序應(yīng)該為您打印一張表格,其中包含姓名、以米為單位的身高、以公斤為單位的體重、您計(jì)算出的 BMI,然后是您 ATM 處于何種 BMI 狀態(tài)的文本。問題是我該怎么做?我剛開始學(xué)習(xí)數(shù)組之類的東西,我的老師給我布置了這個(gè)作業(yè)。我不認(rèn)為這個(gè)作業(yè)很難,只是很難理解該做什么。我已經(jīng)嘗試過的事情是用 String 創(chuàng)建一個(gè) for 循環(huán),名為 name 是這樣的:String name(); 但這顯然行不通。import java.util.Scanner;class Pacient {    public static void main(String args[]){        int pole;        Scanner input = new Scanner(System.in);        String pacient;         System.out.print("Zadej kolik bude pacientu: "); //How many patients do you want? For ex. 10        pacient = input.nextLine();        input.nextLine();        pole = Integer.parseInt(pacient);        String[][] bmi = new String[4][pole]; //This is supposed to make an array with my patients.        double vaha; //weight        double vyska; //height        String jmeno; //name        double telo1, telo2; //body for calc.        String vysledek; //result        int i,x=0,j, pa=0, k=0; //some variables        bmi[0][0] = "Jmeno"; //First line of final table NAME        bmi[0][1] = "Vaha"; // WEIGHT        bmi[0][2] = "Vyska"; //HEIGHT        bmi[0][3] = "BMI"; //BMI based on calc.        bmi[0][4] = "Text"; //Final result        for(int y=1;y<pole;y++){            pa++;            x++;            atm 程序大部分時(shí)間只是打印 NULL NULL NULL NULL,它與患者編號(hào)不匹配。如何將所有這些代碼添加到 for 循環(huán)并使其自動(dòng)將 int 和 double 轉(zhuǎn)換為字符串,然后正確打印它們并將它們分配給 BMI 數(shù)組。如果您有任何進(jìn)一步的問題,請(qǐng)隨時(shí)詢問。
查看完整描述

2 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

我已經(jīng)糾正了代碼中的問題。一切都在評(píng)論中逐步解釋。為了我的理解,我已經(jīng)用英文轉(zhuǎn)換了變量名。如果您有任何疑問。請(qǐng)問。


import java.util.Scanner;


class Pacient {

    private static Scanner input;


    public static void main(String args[]) {

        int numberOfPatients; // Variables that saves number of patient

        // Asking user the number of patients

        input = new Scanner(System.in);

        System.out.print("How many patients do you want?: ");

        // I have change this to nextInt

        // From javadoc "Scans the next token of the input as an int"

        // It is essentially next() + parseInt()

        numberOfPatients = input.nextInt();

        // nextInt() does not move cursor to next line

        // using nextLine() here would move it to next line and close

        // previous line otherwise it creates issue when you will use next/nextLine again

        input.nextLine();


        // String[][] array = new String[Rows][Columns];

        // For each patient there is a row. Since in the code there is header

        // as well that's why we need numberOfPatients + 1

        String[][] bmi = new String[numberOfPatients + 1][5]; 


        // All corresponding columns  

        bmi[0][0] = "Name"; // First line of final table NAME

        bmi[0][1] = "Weight"; // WEIGHT

        bmi[0][2] = "Height"; // HEIGHT

        bmi[0][3] = "BMI"; // BMI based on calc.

        bmi[0][4] = "Result"; // Final result


        // Starting from 1. Skipping header 

        for (int y = 1; y <= numberOfPatients; y++) {

            // Using y instead of an int. This way the loop will

            // automatically move to next row


            // Instead of saving it to variable and then to array 

            // I am saving it directly

            System.out.print("Enter your first name: ");

            bmi[y][0] = input.nextLine();


            System.out.print("Enter your weight in Kg: ");

            bmi[y][1] = input.nextLine();


            System.out.print("Enter your height in m: ");

            bmi[y][2] = input.nextLine();


            // Using the information from above to calculate BMI

            // Basically I am storing and calculating at the same time

            // parseDouble converts String into double 

            // Math.pow(a,b) is powber function. a is base and b is exponent 

            double weight = Double.parseDouble(bmi[y][1]);

            double heightSquare = Math.pow(Double.parseDouble(bmi[y][2]), 2);

            double bmiCalculated = weight / heightSquare;


            // Based on BMI assigning result in result column

            bmi[y][3] = bmiCalculated + "";

            if (bmiCalculated < 18.5) {

                bmi[y][4] = "You are underweight";

            } else if (bmiCalculated > 18.5 && bmiCalculated < 25) {

                bmi[y][4] = "You are normal";

            } else if (bmiCalculated > 25 && bmiCalculated < 30) {

                bmi[y][4] = "You are overweight";

            } else {

                bmi[y][4] = "You are obese";

            }

            System.out.println("Your information has been saved successfully!");

        }


        System.out.println("--------------------------------------------------");


        // In java 2D arrays are multiple 1D array stacked on each other

        // bmi.length gives the number of rows

        // Basically you iterate through each row and print each individual row

        // like 1D array

        for (int i = 0; i < bmi.length; i++) {

        // bmi[i] gives ith row. Which is 1D array. So you can print it like normal array

            for (int j = 0; j < bmi[i].length; j++)

                System.out.print(bmi[i][j] + " ");

            System.out.println();

        }

        System.out.println("--------------------------------------------------");

    }

}



查看完整回答
反對(duì) 回復(fù) 2022-12-28
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的數(shù)組聲明和實(shí)現(xiàn)是沖突的。您已經(jīng)固定了第一個(gè)維度并保留了第二個(gè)變量。但是您使用的好像第一個(gè)是可變的,第二個(gè)是固定的。


你應(yīng)該像這樣聲明你的數(shù)組


String[][] bmi = new String[pole+1][4];


pole+1因?yàn)槟鷮⒌谝恍杏米鞅砀駱?biāo)題


你的第一個(gè)循環(huán)應(yīng)該是這樣的


for(int y = 1; y < pole+1; y++){

    for(int z = 0; z < 4; z++){

        String data="ask user for data";

        bmi[y][z] = data; //similar for all

    }

}

您的輸出for循環(huán)也將如上所示。


查看完整回答
反對(duì) 回復(fù) 2022-12-28
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)