gpt4 book ai didi

java - 循环和重新分配值

转载 作者:行者123 更新时间:2023-12-02 08:22:56 24 4
gpt4 key购买 nike

好吧,如果用户输入某个键,我无法弄清楚如何使我的程序循环回到上一部分。例如,如果他们在任何时候点击 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";}


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

}


}









}

最佳答案

您可以将登录分成较小的函数,而不是在一个巨大的函数中完成所有操作。概要如下

配置函数获取权重输入: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

结束main()

关于java - 循环和重新分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com