Advertisement
mmayoub

Ex07, 19.06.2021

Jun 20th, 2021
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Ex07 {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         int y; // grade entered by the user
  7.         int c = 0; // count valid grades
  8.         int sum = 0;
  9.         double avg;
  10.         double max = 0;
  11.  
  12.         String className;
  13.         int pupilsInClass;
  14.  
  15.         System.out.print("Enter class name: ");
  16.         className = in.next();
  17.         System.out.print("Enter number of pupils in class " + className + ": ");
  18.         pupilsInClass = in.nextInt();
  19.  
  20.         for (int i = 0; i < pupilsInClass; i++) {
  21.             // new student
  22.             System.out.println("* * * New Pupil Data * * *");
  23.             c = 0;
  24.             sum = 0;
  25.  
  26.             // get first grade
  27.             System.out.print("Enter your grade (0 to 100): ");
  28.             y = in.nextInt();
  29.  
  30.             while (y >= 0 && y <= 100) {
  31.                 // valid grade
  32.                 c++; // add 1 to the counter
  33.                 sum = sum + y; // add grade to sum
  34.  
  35.                 // get next grade
  36.                 System.out.print("Enter your grade (0 to 100): ");
  37.                 y = in.nextInt();
  38.             }
  39.  
  40.             System.out.println("You have " + c + " grades");
  41.             System.out.println("The sum of grades is " + sum);
  42.             avg = (double) sum / c;
  43.             System.out.println("The average is " + avg);
  44.  
  45.             if (avg > max) {
  46.                 max = avg;
  47.             }
  48.         }
  49.  
  50.         System.out.println("max class average is " + max);
  51.         in.close();
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement