gpt4 book ai didi

Java - 在赋值时抛出 IllegalArgumentException

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:08 24 4
gpt4 key购买 nike

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

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

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

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 而不是 == ,第二个第一个条件应该是 && 而不是 ||

所以它看起来像这样

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

关于Java - 在赋值时抛出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21803230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com