带有名称,体重,身高,BMI和文本的BMI计算器(使用数组)和循环

时间:2019-05-31 22:15:30

标签: java arrays variables calculator

我想问一问,当周期开始并再次执行该操作时,字符串变量名将增加1。该程序应该询问您要写多少患者。如果你写前。 10,然后该循环将进行10次,它将询问所有我想要的信息,然后将它们添加到我已经创建的称为BMI的阵列中。整个程序应该为您打印一张表格,其中包含名称,以米为单位的身高,以千克为单位的重量,计算出的BMI,然后是您的ATM处于BMI状态的文本。问题是我该怎么办?我刚刚开始学习数组之类的东西,而我的老师给了我这份作业。我认为这项作业并不困难,但很难理解该怎么做。

我已经尝试过的事情是使用名为name的String创建一个for循环,如下所示: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++;

            System.out.print("Zadej svoje krestni jmeno: ");
            jmeno = input.nextLine();

            System.out.print("Zadej svoji vahu v Kg: ");
            vaha = input.nextDouble();

            System.out.print("Zadej svoji vysku v m: ");
            vyska = input.nextDouble();

            System.out.println("Vase informace byly uspesne ulozeny! ");


            bmi[1][0] = jmeno; //These values should somehow increase but idk 
                                 how atm and be assign with the patient which 
                                 will be printed at the end.
            bmi[1][1] = vaha2;
            bmi[1][2] = vyska2;
            bmi[1][3] = telo3;
            bmi[1][4] = vysledek;

        }

        // System.out.println("Tisknu tabulku");

        // telo1 = vyska * vyska; //Some calc. of BMI
        // telo2 = vaha / telo1;


        // if (telo2 < 18.5) { //Adding text to the result variable
        //     vysledek = "mate podvahu";
        // } else if (telo2 < 25) {
        //     vysledek = "Jste v normach";
        // } else if (telo2 < 30) {
        //     vysledek = "Nadvaha";
        // } else {
        //     vysledek = "Obezita";
        // }

        // String telo3 = String.valueOf(telo2); //Converting to strings
        // String vyska2 = String.valueOf(vyska);
        // String vaha2 = String.valueOf(vaha);

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

        for(i=0;i<pole;i++) {
            for(j = 0; j<5; j++) System.out.print(bmi[i][j] + " ");
                System.out.println();
        }
        System.out.println("--------------------------------------------------");
    }
}

Atm程序大部分时间只是在打印NULL NULL NULL NULL NULL,它与患者编号不匹配。如何将所有这些代码添加到for循环中,并使其自动将int和double转换为字符串,然后正确打印它们并将它们分配给BMI数组。如果您还有其他疑问,请随时提问。

2 个答案:

答案 0 :(得分:0)

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

您应该像这样声明数组

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

答案 1 :(得分:0)

我已更正了代码中的问题。注释中将逐步解释所有内容。我已经用英语将变量名转换为我的理解。如果您有任何疑问。请问。

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