好吧我試圖找出如何讓我的程序循環返回到前一節如果用戶輸入某個鍵時遇到問題。例如,如果他們在任何時間點擊w,則程序需要將重量部分放到重量部分,以便他們可以輸入新的重量,並且h爲高度。如果你們可以給我一些建議,我將不勝感激。 謝謝你們。)循環和重新分配值
package Assignments;
進口的java.util。*; 公共類assignment3 {
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
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);
bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
if (bmi < 18.5)
{
classification = "Underweight";
}
else if (bmi < 25)
{
classification = "Normal";
}
else if (bmi < 30)
{
classification = "Overweight";
}
else
{
classification = "Obese";}
System.out.println("Choose Options below to set height and weight");
System.out.println("Your classification is: " + classification);
System.out.println("(H)eight: " + height + " meters");
System.out.println("(W)eight: " + weight + " kilograms");
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
System.out.println("(Q)uit");
String response = stdIn.next();
switch (response.charAt(0)) {
case 'w': response = "Enter new weight: ";
weight = stdIn.nextDouble();
System.out.println("Choose Options below to set height and weight");
System.out.println("Your classification is: " + classification);
System.out.println("(H)eight: " + height + " meters");
System.out.println("(W)eight: " + weight + " kilograms");
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
System.out.println("(Q)uit"); break;
case 'h': response = "Enter new height";
height = stdIn.nextDouble();
System.out.println("Choose Options below to set height and weight");
System.out.println("Your classification is: " + classification);
System.out.println("(H)eight: " + height + " meters");
System.out.println("(W)eight: " + weight + " kilograms");
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
System.out.println("(Q)uit"); break;
case 'q': System.exit(0);
default:
System.out.println (response + "Is not a valid option please try again");
}
}
}
',如果他們打W¯¯隨時方案需要採取的重量section' - 我認爲你需要有回調函數來做到這一點。觸發鍵盤事件並檢查按下的鍵字符並調用相應的方法。 – Mahesh 2011-02-27 00:48:22
你介意給我一個回調的例子嗎?我的教授還沒有教過我們,我無法找到一個例子。 – Brad 2011-02-27 00:57:35