Write a program which prompts user for the number of student

Write a program which prompts user for the number of students, reads it from the keyboard, and saves it in an integer variable. It then prompts the user for the grades of each of the students and saves them in an integer array called grades. Your program should check that the grade is between 0 and 100, if it is not then the user is prompted to enter again. An example is as follow: Enter the number of students: Enter the grade for student 1: 55 Enter the grade for student 2: 108 Invalid grade try again Enter the grade for student 2: 56 Enter the grade for student 3: 57 The average is 56.0

Solution

StudentScoreTest.java

import java.util.Scanner;


public class StudentScoreTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the number of students: \");
       int n = scan.nextInt();
       int grades[] = new int[n];
       int sumScore = 0;
       for(int i=0; i<grades.length; i++){
           System.out.println(\"Enter the grade for the student \"+(i+1)+\": \");
           grades[i] = scan.nextInt();
           if(grades[i] < 0 || grades[i] > 100){
               System.out.println(\"Invalid grade, try again...\");
               i--;
           }
       }
       for(int i=0; i<grades.length; i++){
           sumScore = sumScore + grades[i];
       }
       double average = sumScore/(double)grades.length;
       System.out.println(\"The average is \"+average);
   }

}

Output:

Enter the number of students:
3
Enter the grade for the student 1:
55
Enter the grade for the student 2:
108
Invalid grade, try again...
Enter the grade for the student 2:
56
Enter the grade for the student 3:
57
The average is 56.0

 Write a program which prompts user for the number of students, reads it from the keyboard, and saves it in an integer variable. It then prompts the user for th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site