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
‘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