public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Welcome to GradeCalculator!");
System.out.println("\nPlease enter the number of students: ");
int numberOfStudent = s.nextInt();
System.out.println("Please enter the number of exams: ");
int numberOfExams = s.nextInt();
System.out.println();
//outer loop for the number of students
for (int i = 1; i <= numbeOfStudent; i++) {
System.out.println("----------------------------------------");
System.out.println("Enter student " + i + "'s " + "name: ");
String name = s.nextLine();
s.next();
}
System.out.println();
//inner loop for the number of exams scores entered
int sum = 0;
for (int j = 1; j <= numberOfExam; j++) {
System.out.print("Enter exam scores: ");
double examScore = s.nextDouble();
sum += examScore;
if (examScore < 0) {
System.out.println("Invalid exam scores, reenter: ");
double examScoreReenter = s.nextDouble();
sum += examScoreReenter;
} else {
System.out.println();
}
}
}
console output :
Welcome to GradeCalculator!
Please enter the number of students:
2
Please enter the number of exams:
3
----------------------------------------
Enter student 1's name:
john smith
----------------------------------------
Enter student 2's name:
jane smith
Enter exam scores: "get exception"
------------------------------------------------------------------------
I've been struggling with this for days now and I cannot figure it out. The output I want is this:
-------------------------
Enter student 1's name :
Enter exam score:
Invalid exam scores, reenter:
-------------------------
Any suggestions would be greatly appreciated.