Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java,.equalsIgnoreCase不';两个字不行。我怎么修理?_Java_Java.util.scanner - Fatal编程技术网

使用Java,.equalsIgnoreCase不';两个字不行。我怎么修理?

使用Java,.equalsIgnoreCase不';两个字不行。我怎么修理?,java,java.util.scanner,Java,Java.util.scanner,该程序适用于除高激活和稍微激活外的所有变量。我似乎不明白为什么,但我相信这和.equalsIgnoreCase有关 导入java.util.Scanner; 公共类BMI{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to

该程序适用于除高激活和稍微激活外的所有变量。我似乎不明白为什么,但我相信这和.equalsIgnoreCase有关

导入java.util.Scanner; 公共类BMI{

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Welcome to the BMR calculator!");
    System.out.println("Are you male or female? Enter m or f.");
    String gender = keyboard.next();
    System.out.println("How tall are you in inches?");
    int height = keyboard.nextInt();
    System.out.println("How much do you weigh in pounds?");
    int weight = keyboard.nextInt();
    System.out.println("How old are you in years?");
    int age = keyboard.nextInt();
    System.out.println("How would you describe your lifestyle? \"Sedentary\", \"Somewhat Active\", \"Active\", or \"Highly Active\"?");
    String active = keyboard.next();
        // This gets all the necessary information to calculate the BMI.

    if (gender.equalsIgnoreCase("f")) //If the person is female BMR = 655 + (4.3 x weight in pounds) + (4.7 x height in inches) – (4.7 x age in years) 
    {
        if (active.equalsIgnoreCase("Sedentary"))
        {
            double BMR = (655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)) * 1.2;
            System.out.println("Your BMR is " +BMR+ " calories!");
        }
        ///My issue occurs here I think.
        else if (active.equalsIgnoreCase("Somewhat Active"))

        {
            double BMR = (655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)) * 1.3;
            System.out.println("Your BMR is " +BMR+ " calories!");
        }
        else if (active.equalsIgnoreCase("Active"))
        {
            double BMR = (655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)) * 1.4;
            System.out.println("Your BMR is " +BMR+ " calories!");
        }
        else if (active.equalsIgnoreCase("Highly Active"))
        {
            double BMR = (655 + (4.3 * weight) + (4.7 * height) - (4.7 * age)) * 1.5;
            System.out.println("Your BMR is " +BMR+ " calories!");
        }
    }       
    else if (gender.equalsIgnoreCase("m")) //If the person is male BMR = 66 + (6.3 x weight in pounds) + (12.9 x height in inches) – (6.8 x age in years)
        {
            if (active.equalsIgnoreCase("Sedentary"))
            {
                double BMR = (66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)) * 1.2;
                System.out.println("Your BMR is " +BMR+ " calories!");
            }
            else if (active.equalsIgnoreCase("Somewhat Active"))
            {
                double BMR = (66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)) * 1.3;
                System.out.println("Your BMR is " +BMR+ " calories!");
            }
            else if (active.equalsIgnoreCase("Active"))
            {
                double BMR = (66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)) * 1.4;
                System.out.println("Your BMR is " +BMR+ " calories!");
            }
            else if (active.equalsIgnoreCase("Highly Active"))
            {
                double BMR = (66 + (6.3 * weight) + (12.9 * height) - (6.8 * age)) * 1.5;
                System.out.println("Your BMR is " +BMR+ " calories!");
            }
        }
    }
}
keyboard.next()
仅检索单词
部分
。使用
nextLine
检索整个输入:

int age = keyboard.nextInt();
keyboard.nextLine();
System.out.println("How would you describe your lifestyle? \"Sedentary\", \"Somewhat Active\", \"Active\", or \"Highly Active\"?");
String active = keyboard.nextLine();
keyboard.next()
仅检索单词
部分
。使用
nextLine
检索整个输入:

int age = keyboard.nextInt();
keyboard.nextLine();
System.out.println("How would you describe your lifestyle? \"Sedentary\", \"Somewhat Active\", \"Active\", or \"Highly Active\"?");
String active = keyboard.nextLine();
这是为您准备的。不要将keyboard.next()用于active和skip\n


这是给你的。不要使用键盘。下一步()对于active和skip\n.

我怀疑String.equalsIgnoreCase是否有问题,它和hills一样旧。请根据硬编码字符串仔细检查您的输入。确保它包含您认为它包含的文本。查找额外的空格,使用制表符而不是空格,反之亦然。调试器,或打印st以整数的形式打电话会对您有所帮助。为什么不为您的选项使用编号,这会让您的生活更轻松。例如,1用于久坐等。我怀疑String是否有问题。equalsIgnoreCase,它与hills一样古老。请根据硬编码字符串仔细检查您的输入。确保它包含您认为有问题的文本。请看对于额外的空白,使用制表符而不是空格,反之亦然。调试器或将字符串打印为整数将对您有所帮助。为什么不使用选项编号将使您的生活更加轻松。例如,1用于久坐等。这是有意义的。但如果我将其切换到nextLine()当被问及我的生活方式时,我无法输入任何内容。@NiklasHopper为此,您需要添加一个额外的
键盘。nextLine()
在获得
年龄后
如图所示,阅读键入年龄后输入的新行。太好了!谢谢。我整个上午都在试图解决这个问题。这是有道理的。但是如果我切换到nextLine()当被问及我的生活方式时,我无法输入任何内容。@NiklasHopper为此,您需要在获得
年龄后添加一个额外的
键盘.nextLine();
,如图所示,以阅读您在输入年龄后输入的新行。太好了!谢谢。我整个上午都在努力解决这个问题。