2011-02-27 18 views
0

いいえ私は 'q'にヒットしたときに終了するはずのdo-whileループを持っていますが、代わりにエラーメッセージを表示しています。あなたは何かなどを持っている必要があり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);  // Calculates BMI 
     bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT); // Calculates BSA 


     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(); 

     do { 

      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"); 
       bmi = weight/(height*height);  
       bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT); 
       response = stdIn.next(); 
      } 
      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"); 
       bmi = weight/(height*height);  
       bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT); 
       response = stdIn.next(); 
      } 
      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(); 
      } 
      else if (response.charAt(0) == 'q') 
      { 
       break; 
      } 
     } while (response != "q"); 
    } 
} 
+0

エラーメッセージは何か?文字列比較 –

+0

には、==ではなく '!" q "equals(response) 'を使うべきです。エラーはありませんが、qを入力するとSystem.out.println("これは有効な選択肢ではありません再試行する"); ループの代わりに – Brad

+1

大きなループの最中にすべてのロジックを用意するのではなく、いくつかの小さなメソッドを除外する必要があります。これは、コントロールフローについてより慎重に推論するのに役立ちます。 – andersoj

答えて

0

(response.charAt(0).contains( 'Q'))の場合を試してみてください

1

if (response.charAt(0)== 'w') 
{ 
    ... 
} 
else if(response.charAt(0)== 'h') 
{ 
    ... 
} 
else if(response.charAt(0)== 'q') 
{ 
    System.exit(0); 
} 
else 
{ 
     System.out.println("That is not a valid choice try again"); 
       response = stdIn.next(); 

} 
+0

私はSystem.exit(0)を試しました。 – Brad

+1

コード内で 'else if(response.charAt(0)!= 'w')'または 'else if(response.charAt(0)!= 'h')'なしで試してみてください。 –

2

問題がありますこれらの行:

else if (response.charAt(0)!= 'w') 
// ... 
else if (response.charAt(0)!= 'h') 

応答が「q」である場合、これらのテストのそれぞれが満たされる。それらを取り除く。彼らはあなたが何もしません。代わりに、最後の有効な文字テストの後にプレーンな 'else'を置き、「有効でない選択」プロンプトを表示します。

3

qは、wまたはhと等しくありません。だから、条件はelse if (response.charAt(0)!= 'w')に当てはまるので実際には進まないでしょうelse if (response.charAt(0) == 'q')休憩する条件。

ので、この方法であなたの最後の3 else ifを置く -

else if (response.charAt(0) == 'q') 
    { 
      break; 
    } 
    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(); 
    } 
+0

ああ、働いてくれてありがとう – Brad

+0

また、最後の2つの 'else-if'をelseのような単一の条件にまとめることを考えてみましょうif(' response.charAt(0)!= 'w' || response.charAt(0)!= 'h') '。 else-ifでは、ある文が実行された場合、else-elseの残りの部分は実行されません。 – Mahesh

+0

@ブラッド:無効な選択メッセージを表示するために1つのelseブロックを使用することを検討してください。 – Babar