2011-02-27 49 views
0

我無法使switch語句做我想做的事情。當用戶輸入「w」或「h」時,該陳述應該將他們帶回來進入新的體重或身高,但它並未這樣做。如果有人能指出我做錯了什麼,我將不勝感激。 謝謝我在完善switch語句時遇到問題

package Assignments; 

import 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"; 
    } 

    do { 
     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; 

     default: 
     } 
     System.out.println (response + "Is not a valid option please try again"); 
    } while (stdIn.next().compareToIgnoreCase("q")!=0); 





} 

}

+0

你需要把它包裝成一個循環結構(即while(true)or similar→) – 2011-02-27 02:09:37

回答

0

你需要把你的case語句一個循環中。如果你想使用System.exit()a

while(true){ 

    switch(){ 
     .... 
    } 
} 

將工作。

我建議你讓while循環依賴於用戶輸入什麼不是'q',tho。

+0

好吧我試過把它放在一個do-while循環中,但我仍然沒有得到它的正確工作 – Brad 2011-02-27 02:20:36

2

您從不打印出文字,例如System.out.println(響應),設置響應後=「輸入新的高度」等。