开发者

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
    fin开发者_高级运维al 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);





}

}


You never print out the text, e.g. System.out.println(response), after setting response = "Enter new height", etc.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜