Results 1 to 4 of 4
Thread: Problem with grade calculator?
- 02-15-2012, 12:21 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Problem with grade calculator?
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");
- 02-15-2012, 12:39 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Problem with grade calculator?
When do you think percentage will ever be greater than 1? Suggested reading:Java Code:double percentage = gainedpoints / totalpoints;
Percentage - Wikipedia, the free encyclopedia
Especially:
The percent value is computed by multiplying the numeric value of the ratio by 100
- 02-15-2012, 12:49 AM #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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-15-2012, 01:14 AM #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.
Similar Threads
-
Volume Calculator Problem
By Sathelan in forum AWT / SwingReplies: 1Last Post: 04-19-2011, 04:21 AM -
Problem with NumberFormatException in calculator
By kumarv75 in forum CLDC and MIDPReplies: 17Last Post: 06-28-2010, 12:47 PM -
Java calculator problem ???
By danielmessick in forum Advanced JavaReplies: 2Last Post: 03-13-2010, 06:53 PM -
Time Calculator Problem
By ktisallred in forum New To JavaReplies: 3Last Post: 09-22-2009, 07:15 PM -
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07-06-2008, 04:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks