猿问

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

我想问一下,当循环开始并重新开始时,字符串变量名称将增加 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 数组。如果您有任何进一步的问题,请随时询问。


翻过高山走不出你
浏览 159回答 2
2回答

波斯汪

我已经纠正了代码中的问题。一切都在评论中逐步解释。为了我的理解,我已经用英文转换了变量名。如果您有任何疑问。请问。import java.util.Scanner;class Pacient {&nbsp; &nbsp; private static Scanner input;&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; int numberOfPatients; // Variables that saves number of patient&nbsp; &nbsp; &nbsp; &nbsp; // Asking user the number of patients&nbsp; &nbsp; &nbsp; &nbsp; input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("How many patients do you want?: ");&nbsp; &nbsp; &nbsp; &nbsp; // I have change this to nextInt&nbsp; &nbsp; &nbsp; &nbsp; // From javadoc "Scans the next token of the input as an int"&nbsp; &nbsp; &nbsp; &nbsp; // It is essentially next() + parseInt()&nbsp; &nbsp; &nbsp; &nbsp; numberOfPatients = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; // nextInt() does not move cursor to next line&nbsp; &nbsp; &nbsp; &nbsp; // using nextLine() here would move it to next line and close&nbsp; &nbsp; &nbsp; &nbsp; // previous line otherwise it creates issue when you will use next/nextLine again&nbsp; &nbsp; &nbsp; &nbsp; input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; // String[][] array = new String[Rows][Columns];&nbsp; &nbsp; &nbsp; &nbsp; // For each patient there is a row. Since in the code there is header&nbsp; &nbsp; &nbsp; &nbsp; // as well that's why we need numberOfPatients + 1&nbsp; &nbsp; &nbsp; &nbsp; String[][] bmi = new String[numberOfPatients + 1][5];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // All corresponding columns&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; bmi[0][0] = "Name"; // First line of final table NAME&nbsp; &nbsp; &nbsp; &nbsp; bmi[0][1] = "Weight"; // WEIGHT&nbsp; &nbsp; &nbsp; &nbsp; bmi[0][2] = "Height"; // HEIGHT&nbsp; &nbsp; &nbsp; &nbsp; bmi[0][3] = "BMI"; // BMI based on calc.&nbsp; &nbsp; &nbsp; &nbsp; bmi[0][4] = "Result"; // Final result&nbsp; &nbsp; &nbsp; &nbsp; // Starting from 1. Skipping header&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int y = 1; y <= numberOfPatients; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Using y instead of an int. This way the loop will&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // automatically move to next row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Instead of saving it to variable and then to array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // I am saving it directly&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter your first name: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][0] = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter your weight in Kg: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][1] = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter your height in m: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][2] = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Using the information from above to calculate BMI&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Basically I am storing and calculating at the same time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // parseDouble converts String into double&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Math.pow(a,b) is powber function. a is base and b is exponent&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double weight = Double.parseDouble(bmi[y][1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double heightSquare = Math.pow(Double.parseDouble(bmi[y][2]), 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double bmiCalculated = weight / heightSquare;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Based on BMI assigning result in result column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][3] = bmiCalculated + "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bmiCalculated < 18.5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][4] = "You are underweight";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (bmiCalculated > 18.5 && bmiCalculated < 25) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][4] = "You are normal";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (bmiCalculated > 25 && bmiCalculated < 30) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][4] = "You are overweight";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bmi[y][4] = "You are obese";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Your information has been saved successfully!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("--------------------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; // In java 2D arrays are multiple 1D array stacked on each other&nbsp; &nbsp; &nbsp; &nbsp; // bmi.length gives the number of rows&nbsp; &nbsp; &nbsp; &nbsp; // Basically you iterate through each row and print each individual row&nbsp; &nbsp; &nbsp; &nbsp; // like 1D array&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bmi.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // bmi[i] gives ith row. Which is 1D array. So you can print it like normal array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < bmi[i].length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(bmi[i][j] + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("--------------------------------------------------");&nbsp; &nbsp; }}

宝慕林4294392

您的数组声明和实现是冲突的。您已经固定了第一个维度并保留了第二个变量。但是您使用的好像第一个是可变的,第二个是固定的。你应该像这样声明你的数组String[][] bmi = new String[pole+1][4];pole+1因为您将第一行用作表格标题你的第一个循环应该是这样的for(int y = 1; y < pole+1; y++){&nbsp; &nbsp; for(int z = 0; z < 4; z++){&nbsp; &nbsp; &nbsp; &nbsp; String data="ask user for data";&nbsp; &nbsp; &nbsp; &nbsp; bmi[y][z] = data; //similar for all&nbsp; &nbsp; }}您的输出for循环也将如上所示。
随时随地看视频慕课网APP

相关分类

Java
我要回答