繁体   English   中英

我无法完善switch语句

[英]I'm having trouble perfecting a switch statement

我无法使我的switch语句按照我的意愿行事。 当用户输入'w'或'h'时,该语句应该让他们重新进入以输入新的重量或高度,但它不会这样做。 如果有人能指出我做错了什么我会很感激。 谢谢

package Assignments;

import java.util。*; 公共课作业3 {

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





}

}

在设置response =“Enter new height”等之后,您永远不会打印出文本,例如System.out.println(response)。

你需要把你的case语句放在循环中。 如果要使用System.exit()a

while(true){

     switch(){
         ....
     }
}

将工作。

我建议你使while循环依赖于用户输入任何不是'q'的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM