为了账号安全,请及时绑定邮箱和手机立即绑定

BMI 计算器,包含姓名、体重、身高、BMI 和使用数组和循环的文本

BMI 计算器,包含姓名、体重、身高、BMI 和使用数组和循环的文本

翻过高山走不出你 2022-12-28 10:46:27
我想问一下,当循环开始并重新开始时,字符串变量名称将增加 1。这个程序应该问你要写多少患者。如果你为前任写作。10,然后循环将进行 10 次,它会询问我想要的所有这些信息,然后将它们添加到我已经创建的名为 BMI 的数组中。整个程序应该为您打印一张表格,其中包含姓名、以米为单位的身高、以公斤为单位的体重、您计算出的 BMI,然后是您 ATM 处于何种 BMI 状态的文本。问题是我该怎么做?我刚开始学习数组之类的东西,我的老师给我布置了这个作业。我不认为这个作业很难,只是很难理解该做什么。我已经尝试过的事情是用 String 创建一个 for 循环,名为 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 程序大部分时间只是打印 NULL NULL NULL NULL,它与患者编号不匹配。如何将所有这些代码添加到 for 循环并使其自动将 int 和 double 转换为字符串,然后正确打印它们并将它们分配给 BMI 数组。如果您有任何进一步的问题,请随时询问。
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

我已经纠正了代码中的问题。一切都在评论中逐步解释。为了我的理解,我已经用英文转换了变量名。如果您有任何疑问。请问。


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("--------------------------------------------------");

    }

}



查看完整回答
反对 回复 2022-12-28
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

您的数组声明和实现是冲突的。您已经固定了第一个维度并保留了第二个变量。但是您使用的好像第一个是可变的,第二个是固定的。


你应该像这样声明你的数组


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


pole+1因为您将第一行用作表格标题


你的第一个循环应该是这样的


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循环也将如上所示。


查看完整回答
反对 回复 2022-12-28
  • 2 回答
  • 0 关注
  • 158 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号