Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: Else without If, and ";" Expected in Nested If Statement

I am trying to create a program wherein the program will generate a random number between 0 and 100, and depending on the range, will assign the equivalent Letter Grade to it. I have my If and Else if statements already laid out, but I am getting the following errors on the "else if" lines:

illegal start of type

‘;’ expected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘else’ without ‘if’

I have tried adjusting the locations of the brackets based on how other people have done their nested if Java code online, and even removed the semi-colon.

Code is below:

public class StudentGrade 
{
    public static void main(String[] args)
    {   
        for (int i = 0; i<5; i++)
        { 
            int grade = (int)(Math.random() * 50) + 50;
            if (grade > 0 && grade < 53)
            {
                System.out.println("Student grade is " + grade + " which is F.");
            }
            else if( grade >= 53 && < 74)
            {
                System.out.println("Student grade is " + grade + " which is C.");
            }
            else if (grade >= 74 && < 83)
            {
                System.out.println("Student grade is " + grade + " which is B.");
            }
            else if (grade >= 83 && < 89)
            {
                System.out.println("Student grade is " + grade + " which is B.");
            }
            else if (grade >= 89 && < 100)
            {
                System.out.println("Student grade is " + grade + " which is A.");
            }
        }
    }
}

>Solution :

your if statement syntax was wrong.

if( grade >= 53 && < 74)

if your if statements means grade between 53 to 74. then maybe if statement can be like this:

if ( grade >= 53 && grade < 74 )

and this is more infomation for java if statements

https://www.w3schools.com/java/java_conditions.asp

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading