Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java program that converts number grades to letter grade

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java program that converts number grades to letter grade

    I wrote a java application that coverts number grades to letter grades. Here is what it looks like:

    /java application to That corresponds the letter grade with the number grade
     
    import java.util.Scanner; //program uses the class scanner
     
    public class gradescore{
     
     
    //main method begins execution of java application
     
    public static void main(String args[]){
    	Scanner input=new Scanner(System.in);
     
    	int grade;
     
    	System.out.printf("Entered the numbered grade you received");
    	grade=input.nextInt();
     
    	if(70>=grade>=60)
    	   System.out.println("Your grade is a D"); //java code tells you that your letter grade is a D if you input a score that is between  60 and 70
    	if(80>=grade>=70)
    	   System.out.println("You received a C letter grade");//java code tells you that your letter grade is a C if you input a score that is between and includes 70 and 80
    	if(90>=grade>=80)
    	   System.out.println("You received a B letter grade");//java code tells you that your letter grade is a B if you input a score that is between and includes 90 and 80
    	if(100>=grade>=90)
    	   System.out.println("You received an A letter grade");//java code tells you that your letter grade is a A if you input a score that is between and includes 100 and 90
    	if(60>grade)
    	   System.out.println("You received an F letter grade"); //java code tells you that your letter grade is an F is you input a score that is below 60
     
     
     
     
     
    }//end method
    }//end class gradescore

    Here are the errors that are displayed with I attempt to compile the code.

    C:\Users\Courtney\Desktop\gradescore.java:31: error: '(' expected
    if
    ^
    C:\Users\Courtney\Desktop\gradescore.java:33: error: illegal start of expression
    }//end method
    ^
    C:\Users\Courtney\Desktop\gradescore.java:33: error: ')' expected
    }//end method
    ^
    C:\Users\Courtney\Desktop\gradescore.java:35: error: illegal start of statement
    C:\Users\Courtney\Desktop\gradescore.java:34: error: reached end of file while parsing
    }//end class gradescore
    ^
    5 errors

    Tool completed with exit code 1
    What is wrong with my code?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,289
    My Mood
    Mellow
    Thanks
    191
    Thanked 668 Times in 655 Posts

    Default Re: java program that converts number grades to letter grade

    Your if() statements are not valid. For example:

    if(70>=grade>=60)

    is two conditions written as one:

    grade <= 70, and
    grade >= 60

    The two conditions must be written separately and combined with an AND (&&):

    if ( grade >= 60 && grade <= 70 )

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java program that converts number grades to letter grade

    <nitpick>
    Beware the equals sign. According to your algorithm a grade of 70 will be a C and a D.
    </nitpick>
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: java program that converts number grades to letter grade

    There are several considerations to be made here.

    First, why such long comments on lines that are already printing output? Comments are tools used to point out important logic and reasoning in a program to other programmers - you should assume someone reading your comments knows what an import and a method declaration are. You should comment "why" the code is doing something, rather than "what" it is doing, or the comment will simply be redundant.

    Second, stick to the Java convention of enclosing blocks with curly brackets {}.

    Third, use the else operator. Even if your logic was correct (refer to the above posts), you're performing unnecessary checks -

    if (grade >= 90) {
      System.out.println("You received an A");
    } else if (grade >= 80) {
      System.out.println("You received a B");
    } else if (grade >= 70) {
      System.out.println("You received a C");
    } else if (grade >= 60) {
      System.out.println("You received a D");
    } else {
      System.out.println("You received an F");
    }

    is sufficient.

Similar Threads

  1. Convert Scores into letter Grades
    By noixam123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 12th, 2013, 06:48 AM
  2. [SOLVED] Hangman Program Java: Comparing the User's Guess with Another String Letter by Letter
    By theonlydvr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 29th, 2013, 05:35 PM
  3. Letter grade isn't printing from the function to the output file...HELP!
    By 112297 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: November 22nd, 2012, 10:12 AM
  4. Return letter grade to main method
    By jmanv888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 15th, 2012, 04:48 PM
  5. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 08:14 PM