Java user input and condition checking

Java Program: Take Input from the Student on the Total Marks and Display the Grade Accordingly

The problem is: Write a Java program to display the grade of a student according to the marks inputted by the student. If the marks are in the 90 to 100 range then the grade is O, if in 80 to 89 then the grade is E, if in 70 to 79 then the grade is A, if in the range of 60 to 69 then B,  if in 50 to 59 then the grade is C,  if in 40 to 49 then the grade is D, and if less then 40 then Fail.

In this program, we'll use the Scanner class to receive input of the marks from the student user. So, at first, we have to import the Scanner class from the Java Utility package.

The object of the Scanner class is myObj, which will take the inputs. After receiving the value, we have to check with the if-elseif-else ladder to determine the grade value according to the input marks.

We have to check for the range so in the condition section, it's a must-use AND(&&) between two conditions.

Filename: StudentMarks.java

import java.util.Scanner;
class StudentMarks
{
    public static void main(String args[])
    {
        int marks; //variable to take marks 
        char grade='\0'; //variable to display grade
        
        Scanner myObj = new Scanner(System.in); //Scanner object to take inputs
        System.out.println("Enter your marks:");
        marks = myObj.nextInt(); //to take string use nextInt()
        
        if (marks >= 90 && marks <= 100) // greater than or equal to 90 AND less than or equal to 100
        {
            grade = 'O'; // grade is O if the condition is true
        }
        else if (marks >= 80 && marks < 90) // Range: 80->89
        {
            grade = 'E';
        }
        else if (marks >= 70 && marks < 80) // Range: 60->74
        {
            grade = 'A';
        }
        else if (marks >= 60 && marks < 70) 
        {
            grade = 'B';
        }
        else if (marks >= 50 && marks < 60) 
        {
            grade = 'C';
        }
        else if (marks >= 40 && marks < 50) 
        {
            grade = 'D';
        }
        else if ( marks < 40)
        {
            grade = 'F';
        }
        else
        {
            System.out.println("Wrong Marks");
        }

        System.out.println ("Your Grade = " + grade); 
    }
}

Output:

Java Output of Marks and Grade display