-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamGrade.java
48 lines (43 loc) · 2.1 KB
/
ExamGrade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class ExamGrade{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int highestNumber = 0;
//for loop, so user can enter score 3 times
for(int i = 0; i < 3; i++){
//while loop, so it will repeat until the user has entered a valid number
while(true){
System.out.print("Enter test score " + (i + 1) + ": "); //asking to enter the score
try{
int number = Integer.parseInt(scanner.nextLine()); //converting string to an integer
if(number < 0 || number > 100){ //cheking if the integer is in valid grade boundaries
System.out.println("Number should be greater or equal to 0 and not higher than 100");
}else{
if(number > highestNumber){ //this is needed so highest score can be found
highestNumber = number;
}
break; //to end while loop
}
}catch(NumberFormatException exception){ //handled error if user has entered not valid integer
System.out.println("Not a valid integer");
}
}
}
scanner.close();
//outputting all the data to the user
System.out.println("Highest score was " + highestNumber);
if(highestNumber < 50){
System.out.println("Grade: Fail");
System.out.println("Marks over grade boundary: 0");
}else if(highestNumber <= 59){
System.out.println("Grade: Pass");
System.out.println("Marks over grade boundary: " + highestNumber % 50);
}else if(highestNumber <= 84){
System.out.println("Grade: Merit");
System.out.println("Marks over grade boundary: " + highestNumber % 60);
}else{
System.out.println("Grade: Distinction");
System.out.println("Marks over grade boundary: " + highestNumber % 85);
}
}
}