Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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");

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    planephanatic is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default 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

  1. Volume Calculator Problem
    By Sathelan in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 05:21 AM
  2. Problem with NumberFormatException in calculator
    By kumarv75 in forum CLDC and MIDP
    Replies: 17
    Last Post: 06-28-2010, 01:47 PM
  3. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 07:53 PM
  4. Time Calculator Problem
    By ktisallred in forum New To Java
    Replies: 3
    Last Post: 09-22-2009, 08:15 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 05:01 PM

Posting Permissions

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