BMI 计算器,包含姓名、体重、身高、BMI 和使用数组和循环的文本
BMI Calculator with name, weight, height, BMI and text using Array and for cycle
请问如何在循环开始并重新开始时,字符串变量名称将增加1。这个程序应该问你要写多少个病人。如果你为前任写作。 10,然后循环将进行 10 次,它会询问我想要的所有这些信息,然后将它们添加到我已经创建的名为 BMI 的数组中。整个程序应该为您打印一个 table,其中包含姓名、以米为单位的身高、以公斤为单位的体重、您计算出的 BMI,然后是您 ATM 处于何种 BMI 状态的文本。问题是我该怎么做?我刚开始学习数组之类的东西,我的老师给我布置了这个作业。我不认为这个作业很难,只是很难理解要做什么。
我已经尝试过的事情是用名为 name 的字符串创建一个 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 数组。如果您有任何进一步的问题,请随时提出。
你的数组声明和实现有冲突。您已经固定了第一个维度并保留了第二个变量。但是你使用的好像第一个是可变的,第二个是固定的。
你应该像这样声明你的数组
String[][] bmi = new String[pole+1][4];
pole+1
因为您将第一行用于 table 标题
您的第一个循环应该如下所示
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
循环也将如上所示。
我已经更正了代码中的问题。一切都在评论中解释 step-by-step。为了我的理解,我已经用英文转换了变量名。如果您有任何疑问。请教
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("--------------------------------------------------");
}
}
请问如何在循环开始并重新开始时,字符串变量名称将增加1。这个程序应该问你要写多少个病人。如果你为前任写作。 10,然后循环将进行 10 次,它会询问我想要的所有这些信息,然后将它们添加到我已经创建的名为 BMI 的数组中。整个程序应该为您打印一个 table,其中包含姓名、以米为单位的身高、以公斤为单位的体重、您计算出的 BMI,然后是您 ATM 处于何种 BMI 状态的文本。问题是我该怎么做?我刚开始学习数组之类的东西,我的老师给我布置了这个作业。我不认为这个作业很难,只是很难理解要做什么。
我已经尝试过的事情是用名为 name 的字符串创建一个 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 数组。如果您有任何进一步的问题,请随时提出。
你的数组声明和实现有冲突。您已经固定了第一个维度并保留了第二个变量。但是你使用的好像第一个是可变的,第二个是固定的。
你应该像这样声明你的数组
String[][] bmi = new String[pole+1][4];
pole+1
因为您将第一行用于 table 标题
您的第一个循环应该如下所示
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
循环也将如上所示。
我已经更正了代码中的问题。一切都在评论中解释 step-by-step。为了我的理解,我已经用英文转换了变量名。如果您有任何疑问。请教
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("--------------------------------------------------");
}
}