Java 用户输入失败的if语句,我不知道原因

Java 用户输入失败的if语句,我不知道原因,java,algorithm,loops,Java,Algorithm,Loops,我的程序根据用户的年龄、身高、体重和性别计算并打印BMR值 然而,我的程序在根据输入检查性别时无法工作,直接跳到else语句。 到目前为止,这是我的代码,我如何修复它 import java.util.Scanner; public class MaintainingWeight { public static void main(String[] args) { //Instance Variables int age, weight, inc

我的程序根据用户的年龄、身高、体重和性别计算并打印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

}

好吧,我从来没有真正使用过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"))
{
}


我觉得不错。到底是什么问题?你的问题是什么?@user3189142我想他需要这个等式。他会遇到一些问题,因为BMR是用度量计算的system@user3189142也许不是。还有一些计算器使用US单位。我在循环中添加bmr方程,但当我运行它时,当我输入M或W时,它不会输出bmr。我在做什么wrong@TollesonC编辑了我的答案
if (gender.contains("m") || gender.contains("M"))
{
}
else if (gender.contains("w") || gender.contains("W"))
{
}