[英]Logical Error Java - Wrong Computation in BMI
該程序允許您輸入用戶的身高和體重,然后輸出 BMI 和相關的健康風險。 它將磅轉換為公斤。 它還將英尺和英寸的高度轉換為米。
Scanner scanW = new Scanner (System.in);
Scanner scanH = new Scanner (System.in);
System.out.println("Enter your weight in pounds: ");
int weight = scanW.nextInt();
System.out.println("Enter your height in feet followed \nBy a space then additional inches");
String height = scanH.nextLine();
scanW.close();
scanH.close();
int heightFt = height.charAt(0);
int heightInch = height.charAt(2);
int finalHeightFeet = Integer.parseInt(String.valueOf(heightFt));
int finalHeightInch = Integer.parseInt(String.valueOf(heightInch));
double mass = (double) weight / 2.2;
double finalHeight = (double) (finalHeightFeet * 0.3048) + (finalHeightInch * 0.0254);
double BMI = (double) mass / (finalHeight * finalHeight);
System.out.println("Your BMI is " +BMI);
if (BMI < 18.5)
System.out.println("Your risk category is UnderWeight");
else if (BMI < 25)
System.out.println("Your risk category is Normal Weight");
else if (BMI < 30)
System.out.println("Your risk category is Normal Overweight");
else if (BMI >= 30)
System.out.println("Your risk category is Obese");
正確的 BMI 和風險類別 output 應該是:
Your BMI is 25.013498117367398
Your risk category is Overweight.
但我的 output 會是這樣的:
Your BMI is 0.22261924276759873
Your risk category is UnderWeight
我很確定我的公式有問題,但我似乎找不到。 如果有人指出哪個是錯誤的,那將非常有幫助
您沒有正確解析高度輸入。
假設你輸入
5 9
作為高度。
您將“5 9”分配給height
。
然后你解析
int heightFt = height.charAt(0);
int heightInch = height.charAt(2);
它將53
分配給heightFt
並將57
分配給heightInch
(這些是字符“5”和“9”的數值)。
試試這個:
String[] height = scanH.nextLine().split (" ");
int heightFt = Integer.parseInt (height[0]);
int heightInch = Integer.parseInt (height[1]);
您正在解析以數值表示的chars
。 看看ASCII TABLE 。
例如,如果您將6 2
作為高度,則結果實際上是 6 為54
,空間為32
為50
。
scanW.close(); // these two are not necessary
scanH.close();
int heightInch = height.charAt(2); // what if the person is 5' 11" ?
我修改了你的代碼,看看。
Scanner scanW = new Scanner (System.in);
Scanner scanH = new Scanner (System.in);
System.out.println("Enter your weight in pounds: ");
int weight = scanW.nextInt();
System.out.println("Enter your height in feet followed \nBy a space then additional inches");
String height = scanH.nextLine();
int feets = Integer.parseInt(height.substring(0,1));
// get all the chars after index 2
int inches = Integer.parseInt(height.substring(2));
int totalInches = feets * 12 + inches;
double BMI = weight/Math.pow(totalInches, 2) * 703;
System.out.println("BMI: "+BMI);
System.out.println("Enter your weight in pounds: ");
int weight = scan.nextInt();
System.out.println("Enter your height in feet followed \nBy a space then additional inches");
int heightFt = scan.nextInt();
int heightInch = scan.nextInt();
scan.close();
double finalMass = weight / 2.2;
double finalHeight = (heightFt * 0.3048) + (heightInch * 0.0254);
double BMI = finalMass / (finalHeight * finalHeight);
System.out.println("Your BMI is " +BMI);
if (BMI < 18.5)
System.out.println("Your risk category is UnderWeight");
else if (BMI < 25 && BMI >= 18.5)
System.out.println("Your risk category is Normal Weight");
else if (BMI < 30 && BMI >= 25)
System.out.println("Your risk category is Overweight");
else
System.out.println("Your risk category is Obese");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.