尝试计算GPA
到目前为止,这是我的代码(已更新):
import java.util.Scanner;
public class gpa {
public static void main(String[] args) {
double gpa=0;
double input = 0;
String grade= "";
int classes =0;
double fail =0;
do
{
System.out.print("Enter Grade (n to quit): ");
Scanner sc = new Scanner(System.in);
grade = sc.nextLine();
grade = grade.toLowerCase();
if (grade.equals("a"))
{
input = 4.0;
}
if (grade.equals("b"))
{
input = 3.0;
}
if (grade.equals("c"))
{
input = 2.0;
}
if (grade.equals("d"))
{
input = 1.0;
}
if (grade.equals("f"))
{
fail = 1;
}
gpa+=input;
classes++;
} while(grade!="n");
System.out.print("GPA: " + gpa + " ");
gpa/=classes;
if (gpa>=2 && classes >=4 && fail !=1)
{
System.out.print("Eligible");
}
else if(classes <4)
{
System.out.print("Ineligible. less than 4 classes");
}
else if (gpa < 2.0)
{
System.out.print("Ineligible. GPA is less than 2.0");
}
else if (gpa >=2.0 && fail == 1)
{
System.out.print("Ineligible. GPA is above 2.0, but has an F");
}
else if (gpa <2.0 && fail ==1)
{
System.out.print("Ineligible. GPA is below 2.0 and has F");
}
}
}
这是我需要能够输出的内容:
- 合格
- 不合格,参加少于 4 门课程
- 不合格,gpa 低于 2.0
- 不合格,gpa 高于 2.0 但具有 F 级(注意:gpa >= 2.0)
- 不合格,gpa低于2.0且成绩为F
一直要求输入。我该如何阻止这个? 我尝试将成绩转换为字符,但结果对我来说非常糟糕。 我认为这可能是编译器错误或其他原因。 任何帮助将不胜感激。虽然有用的帮助会降低我的血压。
here's my code so far(it's been updated):
import java.util.Scanner;
public class gpa {
public static void main(String[] args) {
double gpa=0;
double input = 0;
String grade= "";
int classes =0;
double fail =0;
do
{
System.out.print("Enter Grade (n to quit): ");
Scanner sc = new Scanner(System.in);
grade = sc.nextLine();
grade = grade.toLowerCase();
if (grade.equals("a"))
{
input = 4.0;
}
if (grade.equals("b"))
{
input = 3.0;
}
if (grade.equals("c"))
{
input = 2.0;
}
if (grade.equals("d"))
{
input = 1.0;
}
if (grade.equals("f"))
{
fail = 1;
}
gpa+=input;
classes++;
} while(grade!="n");
System.out.print("GPA: " + gpa + " ");
gpa/=classes;
if (gpa>=2 && classes >=4 && fail !=1)
{
System.out.print("Eligible");
}
else if(classes <4)
{
System.out.print("Ineligible. less than 4 classes");
}
else if (gpa < 2.0)
{
System.out.print("Ineligible. GPA is less than 2.0");
}
else if (gpa >=2.0 && fail == 1)
{
System.out.print("Ineligible. GPA is above 2.0, but has an F");
}
else if (gpa <2.0 && fail ==1)
{
System.out.print("Ineligible. GPA is below 2.0 and has F");
}
}
}
Here's what I need to be able to output:
- Eligible
- Ineligible, taking less than 4 classes
- Ineligible, gpa below 2.0
- Ineligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)
- Ineligible, gpa below 2.0 and has F grade
It keeps asking for input. How do I stop this?
I tried converting grade into a char but that ended horribly for me.
I think it might be a compiler error or something.
Any help would be appreciated. Though helpful help would keep my blood pressure down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了损坏的比较(测试两个字符串是否引用同一个对象,而不是它们的内容在逻辑上是否相等)之外,您还无法将成绩的
input
设置为0
F。因此,单个 f 计入 GPA 与之前的成绩相同。当用户最终输入n时,您仍然需要进行数学计算以添加成绩并增加成绩数量。而且,你从来没有真正计算过 GPA(你只是将成绩相加)。
In addition to the broken comparison (testing if two strings reference the same object rather than if their contents are logically equal), you also fail to set
input
to0
for a grade of F. As a result, a single f counts towards the GPA as the same as the previous grade.When the person finally does input n, you still do the math to add the grade and increment the number of grades. Also, you never actually compute the GPA (you just sum the grades).
首先,您不能使用
==
运算符来比较字符串。其作用是比较实际的对象引用,即。对于grade == "a"
它会检查grade
和"a"
是否是相同的对象,但事实并非如此。为了检查字符串的内容是否相等,请使用
.equals()
,即。grade.equals(“a”)
。还有一点,GPA不应该是你总分的平均分吗? IE。完成循环后
gpa /=classes
。另外,不要使用 double 来存储失败(用于了解主题是否失败的标志变量),而是使用 boolean 并将其设置为如果成绩为 F,则为
true
。说到if
语句,它不会修改input
和input< 的先前值/code> 将被添加到
gpa
中,而不是像不及格成绩那样0.0
。那么无效输入怎么办?这还会导致
gpa
增加input
的先前值。编辑:并使用一些
else if
,而不是单独的if
语句,因为即使一个条件通过,也会检查所有其他条件。 IE。如果grade.equals("a")
为 true,您仍然不需要检查grade
是否也是“b”、“c”、“d”和“ f”。EDIT2:根据您刚刚添加的条件列表以及最后一个
if
语句块的方式,一般规则是将更具体的条件放在更通用的条件之上以便它们不会被广义的阴影所掩盖。 IE。如果按照 animuson 提到的那样按顺序排列,则数字 5(“不合格,gpa 低于 2.0,且成绩为 F”)将被数字 3(“不合格,gpa 低于 2.0”)遮盖。For a start, you can't compare strings using the
==
operator. What that does is compare the actual object references, ie. forgrade == "a"
it's checking ifgrade
and"a"
are the same objects, which they're not.In order to check the content of the strings for equality, use
.equals()
, ie.grade.equals("a")
.Another thing, shouldn't GPA be the average of your total scores? ie.
gpa /= classes
after you finish your loop.Also, rather than using a
double
to storefail
, the flag variable for knowing if they failed a subject, use aboolean
and set it totrue
if the grade is F. Speaking of thatif
statement, it doesn't modifyinput
and the previous value ofinput
will be added togpa
instead of a fail grade like0.0
.And what about invalid input? That'll also cause
gpa
to be incremented by the previous value ofinput
.EDIT: And use some
else if
s, instead of separateif
statements, since every other condition will be checked even if one condition passes. ie. Ifgrade.equals("a")
is true, you'll still unnecessarily be checking ifgrade
is also "b", "c", "d" and "f".EDIT2: With that list of conditions you just added and the way you have your last block of
if
statements, a general rule is to have your more specific conditions above the more generalised ones so that they're not shadowed by the generalised ones. ie. Number 5 ("Ineligible, gpa below 2.0 and has F grade") is shadowed by number 3 ("Ineligible, gpa below 2.0") if you keep them in that order, as mentioned by animuson.您修复了除“while”子句之外的所有地方对相等运算符的误用。应该是
You fixed the misuse of the equality operator everywhere but in your "while" clause. It should be
Java、JavaScript、PHP 或 C++,语言无关紧要,因为方法都是相同的。您需要认真考虑在代码中的这两行中使用相同变量的含义:
Java, JavaScript, PHP or C++, the language is irrelevant as the methods are all the same. You need to think seriously about the implications of using the same variable in these two lines in your code:
您使用的是 String (对象)而不是 char,因此您需要使用 .equals 方法。例如,您需要
grade.equals("a")
,而不是grade == "a"
。You're using a String (an object) not a char, so you need to use the .equals method. e.g. instead of
grade == "a"
, you needgrade.equals("a")
.这让很多新的 Java 程序员感到困惑,这是一个非常常见的错误。您需要使用
.equals
方法,而不是==
运算符。请参阅 Java String.equals 与 ==。This trips a lot of new Java programmers up, it's a very common mistake. You need to use the
.equals
method, not the==
operator. See Java String.equals versus ==.