Results 1 to 7 of 7
  1. #1
    Tomj79 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Code check please

    The answer always comes out as a for some reason anyone able to help

    I am not very good at java, so please make your answers simple thank you.

    Java Code:
    //output grade based on percent
    import java.util.*;
    public class Grade
    {
    public static void main (String[] args)
    {
        Scanner gra = new Scanner(System.in);
        System.out.println("Enter the Number of Correct Answers");
        int grade = gra.nextInt();
        System.out.println("Enter the Number of Questions");
        int queshions = gra.nextInt();
        
        int processGrade = queshions / 100 * grade;
    
    if (processGrade <= 70) 
    System.out.println("A");
    
    else if (processGrade > 59) 
    System.out.println("B");
    
    
    else if (processGrade > 49) 
    System.out.println("C");
    
    
    else if (processGrade > 39) 
    System.out.println("D");
    
    else 
    System.out.println("E");
    }
    
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Code check please

    Not much to grade here, its very simplistic code that doesn't do anything really realistic. If it does what you expect it to do, it is fine.

    Arguably I could say: don't write if statements without the accolades, it is very error prone.

    Java Code:
    // do it like this
    if (processGrade <= 70) {
      System.out.println("A");
    }
    But it is correct syntax anyway, so I can't say it is wrong to do it, just inadvisable.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Code check please

    0 is definitely less than 70, so your first condition is definitely true (grade A).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tomj79 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Code check please

    sorry il expand the answers B C D E never come up despite the input

  5. #5
    Tomj79 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Code check please

    omg thanks JosAH feel stupid now

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Code check please

    First, print out the calculated value.
    You're doing integer division, so the odds are good that that number is always 0.

    Second, the way you have your if/else laid out, you'll only ever get A or E.
    Step through it on a piece of paper, presuming you had a processGrade of (say) 60.
    I'm going to guess that that first check (processGrade <= 70) is the wrong way round.

    ETA: And how many times is it possible to be ninja'd??
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Code check please

    oh so this was to ask about a problem with the code :s
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum New To Java
    Replies: 5
    Last Post: 02-07-2012, 03:21 PM
  2. Check out my code
    By ahmedzica in forum New To Java
    Replies: 5
    Last Post: 01-04-2012, 03:07 PM
  3. Code check
    By JustinP in forum New To Java
    Replies: 4
    Last Post: 09-18-2011, 12:07 AM
  4. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  5. Plz Some one check my code
    By TamTam in forum AWT / Swing
    Replies: 1
    Last Post: 02-08-2009, 12:24 AM

Tags for this Thread

Posting Permissions

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