我在完善 switch 语句时遇到困难
我无法让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在设置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.
您需要将 case 语句放入循环内。如果你想使用 System.exit() a
就可以了。
我建议您使 while 循环依赖于用户输入任何非“q”的内容。
you need to put your case statement inside a loop. If you want to use System.exit() a
will work.
I suggest that you make the while loop dependent on the user entering anything NOT a 'q', tho.