gpt4 book ai didi

java - while 循环帮助

转载 作者:行者123 更新时间:2023-12-01 15:55:44 26 4
gpt4 key购买 nike

我有一个类项目,我必须要求用户输入体重指数和体表面积计算器。我有一个 if else 语句,需要放入 while 循环中。我需要让它通过,如果用户输入“w”则显示体重变量,“h”则显示高度。此外,如果用户输入“q”来退出程序。我需要有关如何创建 while 循环的帮助。

import java.util.*;
public class Assignment2 {


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
char quit = stdIn.nextLine().charAt(0);

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

while (quit != 'q');
{
if (bmi < 18.5)
{
classification = "Underweight";
}
else if (bmi < 25)
{
classification = "Normal";
}
else if (bmi < 30)
{
classification = "Overweight";
}
else
{
classification = "Obese";
}

System.out.println("Your classification is: " + classification);

bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
System.out.printf("BMI: %.1f\n", bmi);
System.out.printf("BSA: %.2f\n", bsa);
}
}
}

最佳答案

这是基本逻辑。

   String inp = ask the user for input;
while(!inp.equalsIgnoreCase("q")){
if(inp is w){

}else{
if(inp is h){

}
}
inp = ask user for another input;
}

关于java - while 循环帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5099498/

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