2011-02-27 223 views
0

大家好,我真的很接近完成這一點,但我有我的for循環的問題。我需要我的程序有一個用戶輸入一個「W」或「H」,讓他們進入一個新的體重或身高。當他們做我的循環似乎由於某種原因停止。我do-while循環不循環

package Assignments; 
import java.util.*; 
public class 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"); 

    do { 


     String response = stdIn.next(); 

     if (response.charAt(0)== 'w') 
     { 
      System.out.println("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"); 
     } 
     else if (response.charAt(0) == 'h') 
     { 
      System.out.println("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"); 
     } 
     else if (response.charAt(0)!= 'w') 
     { 
      System.out.println("That is not a valid choice try again"); 
      response = stdIn.next(); 
     } 
     else if (response.charAt(0)!= 'h') 
     { 
      System.out.println("that is not a valid choise try again"); 
      response = stdIn.next(); 
     } 
    } while (stdIn.next().compareToIgnoreCase("q")!=0); 
} 
} 
+0

我的意思是做while while循環對不起 – Brad 2011-02-27 03:24:22

+0

你有沒有想過可能使用Switch Case而不是使用Else IF,這意味着你可以使用While循環來繼續循環,直到你設置一個布爾值,當Case命中'Q 」。只是一個想法。 – 2011-02-27 03:30:35

回答

0

什麼是循環? - 沒關係,因爲它已被糾正

我確實看到了您的掃描儀對象的潛在問題,但因爲它不處理行結束符號,並且您可能想要調用stdIn.nextLine();在每次調用stdIn.next()之後;或stdIn.nextDouble();,只是這樣你才能正確處理行尾字符。

3

這樣做stdIn.next().compareToIgnoreCase("q")!=0你所要求的計算機繪製的下一個值從標準輸入緩衝區。這意味着,你做你的處理後,while循環等待數據從標準輸入進來它決定繼續處理之前。你會具有while(true) { ... } while循環連續運行,並檢查輸入值(當你問的H或W)進行檢查,看它是否是一個Q的更好。如果是,則退出程序。

0

你打電話的地方太多stdIn.next():在循環的開始,在循環終止測試,並在最後兩個分支(其中,順便說一句,都應該由一個else { ... }代替)。 (環路是一個do循環,順便說一下,不for循環。)

的另一個問題是輸入的使用利用的System.out掃描器和輸出的混合。我建議在嘗試通過掃描儀進行輸入之前調用System.out.flush()。