import java.util.Scanner;
public class GradeCheckerSwitch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's score: ");
int score = scanner.nextInt();
char grade = getGradeSwitch(score);
System.out.println("Grade: " + grade);
}
public static char getGradeSwitch(int score) {
int range = score / 10;
switch (range) {
case 10:
case 9:
return 'A';
case 8:
return 'B';
case 7:
return 'C';
case 6:
return 'D';
default:
return 'F';
}
}
}
USING IF -ELSE
import java.util.Scanner;
public class GradeCheckerIfElse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's score: ");
int score = scanner.nextInt();
char grade = getGradeIfElse(score);
System.out.println("Grade: " + grade);
}
public static char getGradeIfElse(int score) {
if (score >= 90 && score <= 100) {
return 'A';
} else if (score >= 80 && score < 90) {
return 'B';
} else if (score >= 70 && score < 80) {
return 'C';
} else if (score >= 60 && score < 70) {
return 'D';
} else if (score >= 0 && score < 60) {
return 'F';
} else {
// Handle invalid score (outside the range 0-100)
System.out.println("Invalid score. Please enter a score between 0 and 100.");
return '\0'; // '\0' is used to represent an undefined character
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter