gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:57:53 26 4
gpt4 key购买 nike

我想问一下,当循环开始并再次循环时,字符串变量名称会增加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,并且与患者编号不匹配。如何将所有这些代码添加到 for 循环中并使其自动将 int 和 double 转换为字符串,然后正确打印它们并将它们分配给 BMI 数组。如果您还有任何疑问,请随时询问。

最佳答案

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

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

关于java - 使用数组和循环的 BMI 计算器,包含姓名、体重、高度、BMI 和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56402268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com