[FIXED] new to java simple question im stuck with on a homework. Trying to get a do while statement to run


First time user of this and pretty new to java programming. I was tasked with making a letter grading do-while statement in class and I’m just having simple issues I believe, but I honestly am confused about what’s wrong.

The outcome Desired is
Enter an integer (1-100): 95
You got an A
Enter an integer (1-100): 85
You got a B
Enter an integer (1-100): 75
You got a C
Enter an integer (1-100):65
You got a D
Enter an integer(1-100):59 (less than 60)
You have failed
The average of these 5 grades is xxx

public class Lettergrade { 
public static void main(String[] args) {        
    boolean check=false;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your grade: ");
    int grade = input.nextint();
    do{    grade = (grade/10);
            if (grade == 9);{{
            System.out.println("Your Letter Grade is: A      ");
            check=(true);}//end elseif
            else if (grade == 8);{
            System.out.println("Your Letter Grade is: B      ");
            check=(true);}//end elseif
            else if (grade == 7);{
            System.out.println("Your Letter Grade is: C      ");
            else if (grade == 6);{
            System.out.println("Your Letter Grade is: D      ");
            }//end elseif
            System.out.println("Your Letter Grade is: F     ");
            }//end elseif
    }//end if
    }while (check==false);//end do
}//end main    
}    //end class


You should learn how to use System.out.println(...) debugging or real debugging in your programs so you can trace it running. Otherwise you are shooting in the dark. See this debugging tutorial.

Your problem seems to be that in Java, if statements ends with the ;. So if (false); { System.out.println("a") } will always print "a". Semicolons are important and you should review the syntax of the Java if statement.

You would have been able to see this if your editor allows you to auto-format your code. For example, my Eclipse IDE spits out:

if (false)

This shows you that the if statement was terminated by the ; and that the curly brace section was not conditional since it wasn’t indented inside of the if.

You also need to be careful about your curly braces. You seem to have an extra one on the 9 conditional line. Again, allowing your editor to auto-format your code may show you these sorts of errors.

Answered By – Gray

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published