简体   繁体   中英

I'm having trouble perfecting a switch statement

I am having trouble making my switch statement do what I want it to. When the user enters a 'w' or a 'h' the statement is supposed to take them back up to enter in a new weight or height but it is not doing this. If anybody can point out to me what I am doing wrong I would appreciate it. Thanks

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

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

you need to put your case statement inside a loop. If you want to use System.exit() a

while(true){

     switch(){
         ....
     }
}

will work.

I suggest that you make the while loop dependent on the user entering anything NOT a 'q', tho.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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