Advertisement
mmayoub

Ex08, 19.06.2021

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