gpt4 book ai didi

java - 我想从 if else 语句中获得更多信息

转载 作者:行者123 更新时间:2023-12-01 17:55:33 24 4
gpt4 key购买 nike

我已经在这个问题上折腾了太久了,希望从 if else 语句中获得更多信息。所有代码都会起作用,直到一个学生的 GPA 和/或 SAT 分数较低,但碰巧是一个大类(class)的告别演说者。我知道这是闻所未闻的,但如果我的代码不符合该学生的资格,那么我的代码就无法正常工作。

我的第一篇文章。感谢您的任何建议布莱恩

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("\n\tStudent Qualifier");

System.out.println("Enter students GPA: ");
double gpa = in.nextDouble();

System.out.println("Enter students SAT score: ");
double sat = in.nextDouble();

System.out.println("\nIf student was valedictorin or salutatorian of a school of 1400 or more, Enter y or n");
String highestHonors = in.next();
in.close();

if (gpa >= 4.0 && sat >= 1100)
studentQualified();
if (gpa >= 3.5 && sat >= 1300)
studentQualified();
if (gpa >= 3.0 && sat >= 1500)
studentQualified();
if (highestHonors == "y")
studentQualified();
else
unQualified();
}

public static void studentQualified() {
System.out.println("Student is qualified");
}
public static void unQualified() {
System.out.println("Student is not qualified");
}

}

最佳答案

您可以使用||来组合您的测试。不要使用 == 来比较 String - 您可以使用 String#equalsIgnoreCase(String)。另外,我强烈建议您始终将 {}if-else 结合使用。就像,

if ((gpa >= 4.0 && sat >= 1100) || (gpa >= 3.5 && sat >= 1300) ||
(gpa >= 3.0 && sat >= 1500) || highestHonors.equalsIgnoreCase("y")) {
studentQualified();
} else {
unQualified();
}

关于java - 我想从 if else 语句中获得更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45205178/

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