Advertisement
mmayoub

Ex09, 19.06.2021

Jun 20th, 2021
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Ex09 {
  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.         int classesNo;
  12.  
  13.         String className;
  14.         int pupilsInClass;
  15.  
  16.         System.out.print("enter the number of the classes: ");
  17.         classesNo = in.nextInt();
  18.  
  19.         for (int j = 0; j < classesNo; j++) {
  20.             // new class
  21.             max = 0;
  22.  
  23.             System.out.print("Enter class name: ");
  24.             className = in.next();
  25.             System.out.print("Enter number of pupils in class " + className + ": ");
  26.             pupilsInClass = in.nextInt();
  27.  
  28.             for (int i = 0; i < pupilsInClass; i++) {
  29.                 // new student
  30.                 System.out.println("* * * New Pupil Data * * *");
  31.                 c = 0;
  32.                 sum = 0;
  33.  
  34.                 // get first grade
  35.                 System.out.print("Enter your grade (0 to 100): ");
  36.                 y = in.nextInt();
  37.  
  38.                 while (y >= 0 && y <= 100) {
  39.                     // valid grade
  40.                     c++; // add 1 to the counter
  41.                     sum = sum + y; // add grade to sum
  42.  
  43.                     // get next grade
  44.                     System.out.print("Enter your grade (0 to 100): ");
  45.                     y = in.nextInt();
  46.                 }
  47.  
  48.                 System.out.println("You have " + c + " grades");
  49.                 System.out.println("The sum of grades is " + sum);
  50.                 avg = (double) sum / c;
  51.                 System.out.println("The average is " + avg);
  52.  
  53.                 if (avg > max) {
  54.                     max = avg;
  55.                 }
  56.             }
  57.  
  58.             System.out.println("max average is " + max);
  59.         }
  60.         in.close();
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement