2011-02-27 4 views
0

switch 문을 원한다면 문제가 있습니다. 사용자가 'w'또는 'h'를 입력하면 명령문을 다시 가져와 새 가중치 또는 높이를 입력해야하지만이 작업은 수행하지 않습니다. 만약 내가 잘못하고있는 것을 누군가가 나에게 지적 할 수 있다면, 나는 그것을 고맙게 생각할 것이다. 감사switch 문을 완성하는 데 문제가 있습니다.

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

    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) 또는 유사 ...) –

답변

0

당신은 루프 내부에 case 문을 넣어해야합니다. System.exit()을 사용하려면

while(true){ 

    switch(){ 
     .... 
    } 
} 

이 효과적입니다.

사용자가 while 루프를 'q'가 아닌 아무 것도 입력하지 않아도되도록 제안하는 것이 좋습니다.

+0

Ok do-while 루프 안에 넣으 려하지만 여전히 제대로 작동하지는 않습니다. – Brad

2

텍스트를 인쇄하지 않습니다. 예 : System.out.println (응답), 응답 = "새 높이 입력"등을 설정 한 후

관련 문제