1. Member
Join Date
Feb 2012
Posts
3
Rep Power
0

Why does this portion of code end up only displaying the else statement? Thanks for reading, and possibly helping.

Java Code:
```		System.out.printf("[Total Points:]");
Scanner s_one = new Scanner(System.in);
int totalpoints = s_one.nextInt();

System.out.printf("[Points Gained:]");
Scanner s_two = new Scanner(System.in);
int gainedpoints = s_two.nextInt();

double percentage = gainedpoints / totalpoints;

if (percentage >= 93) {
System.out.printf("A");
}
else if (percentage >= 85) {
System.out.printf("B");

}
else if (percentage >= 75) {
System.out.printf("C");

}
else if (percentage >= 67) {
System.out.printf("D");

}
else {
System.out.printf("F");
}
System.out.printf("\nThis should print\n");```

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
13

## Re: Problem with grade calculator?

Java Code:
`double percentage = gainedpoints / totalpoints;`
When do you think percentage will ever be greater than 1? Suggested reading:
Percentage - Wikipedia, the free encyclopedia
Especially:
The percent value is computed by multiplying the numeric value of the ratio by 100

3. ## Re: Problem with grade calculator?

Integer division truncates the result. If gainedpoints is less than totalpoints, gainedpoints / totalpoints will always be zero. To retain the fractional part to assign it to a couble, you need to cast either the dividend or the divisor to double.

But a quotient is not a percentage.

Additionally, you needn't (and shouldn't) construct a new Scanner for each read. One is enough.

db

4. Member
Join Date
Feb 2012
Posts
1
Rep Power
0

## Re: Problem with grade calculator?

The previous two posts are absolutely correct. I don't normally hand out code, so I'm going to suggest that you review the working of percentages. As doWhile pointed out, the resultant values are going to be between zero and one rather the zero and one-hundred frame specified in your if statements.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•