我一直在爲我的java類做一個項目。對於該項目,我必須讓用戶輸入輸入並計算他們的體重指數和體表面積,程序應該保持運行直到用戶輸入「q」。當「q」被放入時,我無法讓程序停止運行,只是崩潰。另外我對Java和編程一般都很新,所以我會很感激任何幫助。我的代碼如下。 感謝:)按'q'結束for循環
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
String weight; // Weight in kilograms
String height; // Height in meters
String classification; // Classifies the user into BMI categories
double bsa; // Body surface area
do {
System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
weight = stdIn.next();
System.out.print("Enter height in meters: ");
height = stdIn.next();
double height2 = Double.parseDouble(height);
double weight2 = Double.parseDouble(weight);
bmi = weight2/(height2*height2);
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(((height2*METERS_TO_CM)*weight2)/BSA_CONSTANT);
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
System.out.println("Hit 'q' to quit");
} while (stdIn.nextLine().compareToIgnoreCase("q")==0);
}
}
這些天,誰家將有一個關於計算BMI的問題,它不會做作業。 :-) – corsiKa 2011-02-24 23:17:21
是啊,現在我只是擔心功課:) – Brad 2011-02-24 23:19:23
哦,這不是什麼反對你,或問題。這是每個人在一個班級或另一個班級中做的「經典」作業作業之一。我只知道其中一天,一些對編程一無所知的保險人將會試圖爲他的保險報告製作BMI計算器,並且人們會認爲它是功課。呵呵呵。 – corsiKa 2011-02-24 23:26:14