Reputation: 45
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:
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.
Upvotes: 2
Views: 5055
Reputation: 1
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:
else if (gpa <2.0 && fail ==1)
Upvotes: -1
Reputation: 2217
You fixed the misuse of the equality operator everywhere but in your "while" clause. It should be
while(!grade.equals("n")
Upvotes: 0
Reputation: 18502
For a start, you can't compare strings using the ==
operator. What that does is compare the actual object references, ie. for grade == "a"
it's checking if grade
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 store fail
, the flag variable for knowing if they failed a subject, use a boolean
and set it to true
if the grade is F. Speaking of that if
statement, it doesn't modify input
and the previous value of input
will be added to gpa
instead of a fail grade like 0.0
.
And what about invalid input? That'll also cause gpa
to be incremented by the previous value of input
.
EDIT: And use some else if
s, instead of separate if
statements, since every other condition will be checked even if one condition passes. ie. If grade.equals("a")
is true, you'll still unnecessarily be checking if grade
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.
Upvotes: 1
Reputation: 182819
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
to 0
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).
Upvotes: 1
Reputation: 48795
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 ==.
Upvotes: 1
Reputation: 578
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 need grade.equals("a")
.
Upvotes: 1