mmayoub

Ex12, 19.06.2021

Jun 20th, 2021 (edited)
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Ex12 {
  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.         int excelent = 0;
  17.  
  18.         int excelentInClass = 0;
  19.         int maxExcelentInClass = 0;
  20.         String maxClassName = "";
  21.  
  22.         System.out.print("enter the number of the classes: ");
  23.         classesNo = in.nextInt();
  24.  
  25.         for (int j = 0; j < classesNo; j++) {
  26.             // new class
  27.             max = 0;
  28.             excelentInClass = 0;
  29.  
  30.             System.out.print("Enter class name: ");
  31.             className = in.next();
  32.             System.out.print("Enter number of pupils in class " + className + ": ");
  33.             pupilsInClass = in.nextInt();
  34.  
  35.             for (int i = 0; i < pupilsInClass; i++) {
  36.                 // new student
  37.                 System.out.println("* * * New Pupil Data * * *");
  38.                 c = 0;
  39.                 sum = 0;
  40.  
  41.                 // get first grade
  42.                 System.out.print("Enter your grade (0 to 100): ");
  43.                 y = in.nextInt();
  44.  
  45.                 while (y >= 0 && y <= 100) {
  46.                     // valid grade
  47.                     c++; // add 1 to the counter
  48.                     sum = sum + y; // add grade to sum
  49.  
  50.                     // get next grade
  51.                     System.out.print("Enter your grade (0 to 100): ");
  52.                     y = in.nextInt();
  53.                 }
  54.  
  55.                 System.out.println("You have " + c + " grades");
  56.                 System.out.println("The sum of grades is " + sum);
  57.                 avg = (double) sum / c;
  58.                 System.out.println("The average is " + avg);
  59.  
  60.                 if (avg > max) {
  61.                     max = avg;
  62.                 }
  63.  
  64.                 if (avg > 92) {
  65.                     excelentInClass++;
  66.                     excelent++;
  67.                 }
  68.             }
  69.             System.out.println("excelent in class are: " + excelentInClass);
  70.             System.out.println("max average is " + max);
  71.  
  72.             if (excelentInClass > maxExcelentInClass) {
  73.                 maxExcelentInClass = excelentInClass;
  74.                 maxClassName = className;
  75.             } else if (excelentInClass == maxExcelentInClass) {
  76.                 maxExcelentInClass = excelentInClass;
  77.                 maxClassName = maxClassName + ", " + className;
  78.             }
  79.         }
  80.         System.out.println("the number of excelent pupils is " + excelent);
  81.         System.out.println("max number of excelent pupils is class is " + maxExcelentInClass);
  82.         System.out.println("max excelent pupils are in class " + maxClassName);
  83.         in.close();
  84.     }
  85. }
  86.  
Add Comment
Please, Sign In to add comment