我必须在Java中为一个任务创建一个身体质量指数计算器,并且如果用户在要求重量时输入字母字符,我希望程序退出并显示一个信息。我需要帮助创建if语句。这是我的(这不工作)。我尝试了各种其他的不平等符号,但没有成功。我想我可能不得不使用一个if(在命令之间),但我没有发现一种方法来使用权重变量的工作。当输入一个字母字符时退出java程序
我收到该错误是如下 Lab4.java:21:错误不能找到符号 如果(重量< = Z) ^
public class Lab4
{
public static void main (String[] args)
{
Scanner kb = new Scanner (System.in);
System.out.println("enter e for english or m for metric");
String choice;
choice = kb.nextLine();
char firstchar;
firstchar = choice.charAt(0);
boolean english;
english = firstchar == 'e';
if (english)
{
// prompt the user to input their weight in pounds
System.out.println("Enter your weight in pounds");
double weight = kb.nextDouble();
if (weight <= Z)
{
System.out.println("letters are not an acceptable weight");
System.exit(0);
}
// prompt the user to input their height in inches
System.out.println("Enter your height in inches");
double height = kb.nextDouble();
if (height == 0.0)
{
System.out.println("0 is not a valid height");
System.exit(0);
}
// make bmi to an integer and compute bmi by dividing weight by height^2 * 750
int bmi = (int)(weight/(height*height)*703);
// have the computer display height, wieght, and bmi
System.out.printf("your weight is %8.2f", weight);
System.out.println(" pounds");
System.out.printf("your height is %8.2f", height);
System.out.println(" inches");
System.out.println("your BMI is " + bmi);
if (bmi <= 24)
System.out.println("Normal Bodyweight");
else
{
if (bmi > 30)
System.out.println("Obese");
else
System.out.println("Overweight");
请包含更多上下文,特别是您的变量声明和错误消息。 – shmosel
“错误找不到符号if(weight <= Z)”。那是因为“Z”没有被定义。 –