【问题标题】:User input failing if statement and I don't know why [closed]用户输入失败 if 语句,我不知道为什么[关闭]
【发布时间】:2014-04-25 01:50:36
【问题描述】:

我的程序正在根据用户的年龄、身高、体重和性别计算并打印用户的 BMR 值。

但是,在根据输入检查性别并直接跳转到 else 语句时,我的程序无法正常工作。 这是我目前的代码,我该如何解决?

import java.util.Scanner;
public class MaintainingWeight
{
    public static void main(String[] args) 
    {
        //Instance Variables
        int age, weight, inches; //weight in pounds, height inches
        double BMR;
        String gender;

        //Program Introduction Welcoming the user
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Hello fellow user, this program will help determine \nyour BMR value and tell the amount of chocalate bars you will need");
        System.out.println("in order to maintain your body weight.");

        //Ask the User to input their age, weight and height
        System.out.println("Please enter your age.");
        age = keyboard.nextInt();

        System.out.println("Next enter your weight in pounds");
        weight = keyboard.nextInt();

        System.out.println("Now enter your height by inches\n (Reminder: to find your height in inches mutiply your height by feet to 12.000. (feet * 12.000))");
        inches = keyboard.nextInt();

        //Tell the user their gender then print their BMR Value
        System.out.println("Please enter your gender. Enter M as Men or W as women");
        gender = keyboard.nextLine();

        if (gender.equalsIgnoreCase("M"))
        {
            System.out.println("Your BMR value is: ");
        }
        else if (gender.equalsIgnoreCase("W"))
        {
            System.out.println("Your BMR Value is: ");
        }
        else
        {
            System.out.println();
        }

    }

    //Print to the user the amount of chocolate bars they need to consume to maintain their weight

}

【问题讨论】:

  • 这对我来说很好。究竟是什么问题?
  • 你的问题是什么?
  • @user3189142 我认为他需要方程式
  • 他会遇到一些问题,因为 BMR 是用公制计算的
  • @user3189142 可能不是。还有一些计算器使用美制单位。

标签: java algorithm loops


【解决方案1】:

好吧,我从来没有真正使用过java,如果我犯了任何编码错误,请原谅我......

if (gender.equalsIgnoreCase("M"))
{
    System.out.print("Your BMR value is: "); //Prints without a newline I assume
    System.out.print((66 + (13.8 * (weight * 0.454)) + (5 * (height * 2.54)) - (6.8 * age));
}
else if (gender.equalsIgnoreCase("W"))
{
    System.out.print("Your BMR value is: "); //Prints without a newline I assume
    System.out.print((655 + (9.6 * (weight * 0.454)) + (1.8 * (height * 2.54)) - (4.7 * age));
}
else
{
    System.out.println();
}

好的,问题是keyboard.nextLine() 在末尾添加了一个\n。两种修复方法:

if (gender.equalsIgnoreCase("M\n"))
{
}
else if (gender.equalsIgnoreCase("W\n"))
{
}

if (gender.contains("m") || gender.contains("M"))
{
}
else if (gender.contains("w") || gender.contains("W"))
{
}

【讨论】:

  • 我在循环中添加了 bmr 方程,但是当我运行它时,当我输入 M 或 W 时它不会输出 BMR。我做错了什么
  • @TollesonC 编辑了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2015-07-12
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多