Java 循环和重新分配值
好的,如果用户输入某个键,我在试图弄清楚如何使我的程序循环回到上一节时遇到困难。例如,如果他们在任何时候点击了w,程序需要转到“重量”部分,这样他们就可以输入新的重量,h也可以输入新的重量作为身高。如果你们能给我一些建议,我将不胜感激。 谢谢大家:) 导入java.util.*; 公开课作业3{Java 循环和重新分配值,java,loops,Java,Loops,好的,如果用户输入某个键,我在试图弄清楚如何使我的程序循环回到上一节时遇到困难。例如,如果他们在任何时候点击了w,程序需要转到“重量”部分,这样他们就可以输入新的重量,h也可以输入新的重量作为身高。如果你们能给我一些建议,我将不胜感激。 谢谢大家:) 导入java.util.*; 公开课作业3{ public static void main(String[] args) { //Scanner Scanner stdIn = new Scanner(System.in);
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";}
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;
case 'q': System.exit(0);
default:
System.out.println (response + "Is not a valid option please try again");
}
}
}
publicstaticvoidmain(字符串[]args){
//扫描器
扫描仪标准输入=新扫描仪(System.in);
//变数
final double METERS_TO_CM=100;//将米转换为厘米的常数
最终双BSA_常数=3600;//BSA除以的常数
双倍体重指数;//体重指数
双倍重量;//重量单位为千克
双倍高度;//以米为单位的高度
字符串分类;//将用户分类为BMI类别
双bsa;//体表面积
System.out.print(“欢迎使用BMI和BSA计算器开始以千克为单位输入体重”);
重量=stdIn.nextDouble();
系统输出打印(“以米为单位输入高度:”;
高度=stdIn.nextDouble();
体重指数=体重/(身高*身高);
bsa=数学sqrt((高度*米到厘米)*重量)/bsa_常数);
如果(体重指数<18.5)
{
分类=“体重不足”;
}
否则,如果(体重指数<25)
{
分类=“正常”;
}
否则,如果(体重指数<30)
{
分类=“超重”;
}
其他的
{
分类=“肥胖”;}
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
系统输出打印项次(“(Q)uit”);
字符串响应=stdIn.next();
开关(响应字符(0)){
案例“w”:响应=“输入新权重:”;
重量=stdIn.nextDouble();
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
System.out.println(“(Q)uit”);中断;
案例“h”:响应=“输入新高度”;
高度=stdIn.nextDouble();
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
System.out.println(“(Q)uit”);中断;
案例“q”:系统退出(0);
违约:
System.out.println(response+“不是有效选项,请重试”);
}
}
}
您可以将登录分离为更小的功能,而不是在一个巨大的功能中完成所有操作。这是提纲
用于获取权重输入的污损函数:double getWeight()
获取高度输入的污损函数:double getHeight()
定义函数进行计算
定义函数以显示结果
main()
getWeight()
getHeight()
doMath()
showResult()
Loop
show options (H/W/Q)
switch
case H:
getHeight()
doMath()
showResult()
case W
getWeight()
doMath()
showResult()
case Q
Exit program
end switch
end Loop
End main()
如果它们在任何时候点击w,程序需要将其转到权重部分
-我认为您需要使用回调函数来完成此操作。正在触发一个键盘事件,检查按下的键字符并调用相应的方法。请您给我看一个回调的快速示例。我的教授还没有教我们这个,我很难找到一个例子。这看起来是一个很好的解决办法,我会试试。非常感谢你。我在想海报可能希望把命令模式作为参考,好吧,我把它放在一个switch语句中,它看起来是这样的,但是当我输入h或w时,它什么都不做
main()
getWeight()
getHeight()
doMath()
showResult()
Loop
show options (H/W/Q)
switch
case H:
getHeight()
doMath()
showResult()
case W
getWeight()
doMath()
showResult()
case Q
Exit program
end switch
end Loop