using the while loop, and scenner draft a java code that grades students when they enter makes it grades it with A,B+,B,C,C+ and F grades
Question
using the while loop, and scenner draft a java code that grades students when they enter makes it grades it with A,B+,B,C,C+ and F grades
Solution
Here's a Java code that uses a while
loop and a Scanner
to grade students based on their scores. The grading system assigns grades as A, B+, B, C+, C, and F.
import java.util.Scanner;
public class StudentGrading {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score;
while (true) {
System.out.print("Enter the student's score (0-100) or -1 to exit: ");
score = scanner.nextInt();
// If the user inputs -1, exit the loop
if (score == -1) {
break;
}
// Validate the score range
if (score < 0 || score > 100) {
System.out.println("Invalid score! Please enter a score between 0 and 100.");
continue; // Go back to the beginning of the loop
}
// Determine the grade based on the score
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 85) {
grade = "B+";
} else if (score >= 80) {
grade = "B";
} else if (score >= 75) {
grade = "C+";
} else if (score >= 70) {
grade = "C";
} else {
grade = "F";
}
// Output the grade
System.out.println("The student's grade is: " + grade);
}
// Close the scanner
scanner.close();
System.out.println("Grading session ended.");
}
}
Explanation:
- The program uses a
while
loop that continuously prompts the user to enter student scores until they input-1
to exit. - It checks if the input score is valid (between 0 and 100). If the input is invalid, it prompts the user again.
- Based on the provided score, it determines the grade using conditional statements.
- It outputs the corresponding grade for each entered score and terminates the loop upon entering
-1
.
You can compile and run this Java program to grade students based on their scores.
Similar Questions
Java Program To Accept the Marks of a Student and Find the Total Marks and Percentage
evelop a Java program that calculates the average of three floating-pointnumbers and displays a grade based on the average.
Develop a Java program to add student’s names and display it using vectorclass.
Enter the student details using constructors with arguments. Then find the total marks of each student. If it is greater than 500 print pass ,else print fail.
What is the output of the following code snippet?int i = 0;do { if (i == 3) continue; System.out.print(i + " "); i++;} while (i < 5);
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.