mmayoub

Ex06, 19.06.2021

Jun 20th, 2021
691
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Ex06 {
  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.  
  11.         String className;
  12.         int pupilsInClass;
  13.  
  14.         System.out.print("Enter class name: ");
  15.         className = in.next();
  16.         System.out.print("Enter number of pupils in class " + className + ": ");
  17.         pupilsInClass = in.nextInt();
  18.  
  19.         for (int i = 0; i < pupilsInClass; i++) {
  20.             // new student
  21.             System.out.println("* * * New Pupil Data * * *");
  22.             c = 0;
  23.             sum = 0;
  24.  
  25.             // get first grade
  26.             System.out.print("Enter your grade (0 to 100): ");
  27.             y = in.nextInt();
  28.  
  29.             while (y >= 0 && y <= 100) {
  30.                 // valid grade
  31.                 c++; // add 1 to the counter
  32.                 sum = sum + y; // add grade to sum
  33.  
  34.                 // get next grade
  35.                 System.out.print("Enter your grade (0 to 100): ");
  36.                 y = in.nextInt();
  37.             }
  38.  
  39.             System.out.println("You have " + c + " grades");
  40.             System.out.println("The sum of grades is " + sum);
  41.             avg = (double) sum / c;
  42.             System.out.println("The average is " + avg);
  43.         }
  44.  
  45.         in.close();
  46.     }
  47. }
  48.  
RAW Paste Data