Java-在赋值时抛出IllegalArgumentException

Java-在赋值时抛出IllegalArgumentException,java,Java,我正在从事一项Java作业,其中给了我一套指导原则,并要求我正确编写一个程序,根据性别、体重、身高和年龄,计算每周进行1-3天轻度运动/锻炼的人的每日卡路里需求量 作业的要点之一是学习异常的使用。我已经编写了代码,并且编译正确。然而,当运行时,我总是抛出我的异常,即使在性别变量设置为F、F、M或M时也是如此。任何提示都是很好的提示,但我想知道如何启动这个程序 为了澄清,所有的评论都来自老师,而所有的代码都来自我。我使用注释作为代码的框架。另外,通过老师的评论,您会注意到我应该有另一个抛出的关于c

我正在从事一项Java作业,其中给了我一套指导原则,并要求我正确编写一个程序,根据性别、体重、身高和年龄,计算每周进行1-3天轻度运动/锻炼的人的每日卡路里需求量

作业的要点之一是学习异常的使用。我已经编写了代码,并且编译正确。然而,当运行时,我总是抛出我的异常,即使在性别变量设置为F、F、M或M时也是如此。任何提示都是很好的提示,但我想知道如何启动这个程序

为了澄清,所有的评论都来自老师,而所有的代码都来自我。我使用注释作为代码的框架。另外,通过老师的评论,您会注意到我应该有另一个抛出的关于calculateCalories()中其他参数的IllegaRugmentException。我还没有这样做,因为我仍在努力使性别部分正确

import java.util.*;

//add class javadoc comment here
public class CalorieCalculator {

public static final double LIGHT_ACTIVITY_FACTOR = 1.375;
public static final int MALE_BMR_CONSTANT = 66;
public static final int FEMALE_BMR_CONSTANT = 655;
public static final double MALE_WEIGHT_COEFFICIENT = 6.23;
public static final double FEMALE_WEIGHT_COEFFICIENT = 4.35;
public static final double MALE_HEIGHT_COEFFICIENT = 12.7;
public static final double FEMALE_HEIGHT_COEFFICIENT = 4.7;
public static final double MALE_AGE_COEFFICIENT = 6.8;
public static final double FEMALE_AGE_COEFFICIENT = 4.7;

//Prompt the user for gender (m,f,M,F), weight(lbs, double),
//height(inches, integer), and age(yrs, integer).
//You can assume the user will type in a number for weight
//and an integer for height and age - it is okay if the
//program crashes if they don't.
//Pass the user supplied values to the
//calculateCalories() method and output the returned daily calorie
//requirement (with descriptive text).

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);

    System.out.print("Please enter your gender: ");
    String gender1 = console.next();
    System.out.print("Please enter your weight in pounds: ");
    double weight1 = console.nextDouble();
    System.out.print("Please enter your height in inches: ");
    int height1 = console.nextInt();
    System.out.print("Please enter your age in years: ");
    int age1 = console.nextInt();

    calculateCalories(gender1, weight1, height1, age1);


    System.out.println("Women:");
    System.out.println("Daily Calorie Requirement for Light Activity = " + (calculateCalories(gender1, weight1, height1, age1)));

}


//Throw an IllegalArgumentException if gender is not
//"f", "F", "m", or "M" or if any of the other parameters
//are not greater than 0. The exception message should
//explain the error.
//Use the formulas given below to calculate and return 
//the daily calorie requirement.

public static double calculateCalories(String gender, double weight, int height, int age) {
    if (gender.toLowerCase() != "m" || gender.toLowerCase() != "f") {
        throw new IllegalArgumentException("Please enter either M, F, m, or f");
    } else {

        if (gender == "f" || gender == "F") {
            double bmrFemale = (FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight) + (FEMALE_HEIGHT_COEFFICIENT * height) - (FEMALE_AGE_COEFFICIENT * age));
            return LIGHT_ACTIVITY_FACTOR * bmrFemale; 
        } else {
            double bmrMale = (MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight) + (MALE_HEIGHT_COEFFICIENT * height) - (MALE_AGE_COEFFICIENT * age));
            return LIGHT_ACTIVITY_FACTOR * bmrMale;
        }
    }

}

首先,您需要在条件中使用.equal而不是==, 第二,第一个条件应为ith&¬||

所以看起来是这样的

if (!gender.toLowerCase().equal("m") && !gender.toLowerCase().equal("f") {
        throw new IllegalArgumentException("Please enter either M, F, m, or f");

不要使用
==
比较字符串值。使用
equals()
。谢谢你们两位。很好的洞察力。