我在完善 switch 语句时遇到困难

发布于 2024-10-19 09:46:19 字数 3108 浏览 3 评论 0原文

我无法让我的 switch 语句执行我想要的操作。当用户输入“w”或“h”时,该语句应该将它们返回以输入新的体重或身高,但它并没有这样做。如果有人能向我指出我做错了什么,我将不胜感激。 感谢

package Assignments;

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





}

}

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





}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-10-26 09:46:19

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

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

来日方长 2024-10-26 09:46:19

您需要将 case 语句放入循环内。如果你想使用 System.exit() a

while(true){

     switch(){
         ....
     }
}

就可以了。

我建议您使 while 循环依赖于用户输入任何非“q”的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文