【发布时间】:2011-02-24 11:07:26
【问题描述】:
我有一个课程项目,我必须要求用户输入体重指数和体表面积计算器。我有一个 if else 语句,我需要将其放入一个 while 循环中。我需要让它通过,如果用户输入“w”,则显示重量变量和“h”表示高度。此外,如果用户输入“q”以退出程序。我需要有关如何创建 while 循环的帮助。
import java.util.*;
public class Assignment2 {
public static void main(String[] args) {
//Scanner
Scanner stdIn = new Scanner(System.in);
//Variables
final double METERS_TO_CM = 100; // The constant to convert meters to centimeters
final double BSA_CONSTANT = 3600; // The constant to divide by for bsa
double bmi; // Body Mass Index
double weight; // Weight in kilograms
double height; // Height in meters
String classification; // Classifies the user into BMI categories
double bsa; // Body surface area
char quit = stdIn.nextLine().charAt(0);
System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
weight = stdIn.nextDouble();
System.out.print("Enter height in meters: ");
height = stdIn.nextDouble();
bmi = weight/(height*height);
while (quit != 'q');
{
if (bmi < 18.5)
{
classification = "Underweight";
}
else if (bmi < 25)
{
classification = "Normal";
}
else if (bmi < 30)
{
classification = "Overweight";
}
else
{
classification = "Obese";
}
System.out.println("Your classification is: " + classification);
bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
}
}
}
【问题讨论】:
-
您忘记再次发布完整代码。 create a new question值得吗?我不知道。我建议阅读更多文档。
标签: java while-loop