Advertisement
mmayoub

Ex04, 19.16.2021

Jun 20th, 2021
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Ex04 {
  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.         // get first grade
  12.         System.out.print("Enter your grade (0 to 100): ");
  13.         y = in.nextInt();
  14.  
  15.         while (y >= 0 && y <= 100) {
  16.             // valid grade
  17.             c++; // add 1 to the counter
  18.             sum = sum + y; // add grade to sum
  19.  
  20.             // get next grade
  21.             System.out.print("Enter your grade (0 to 100): ");
  22.             y = in.nextInt();
  23.         }
  24.  
  25.         System.out.println("You have " + c + " grades");
  26.         System.out.println("The sum of grades is " + sum);
  27.         avg = (double) sum / c;
  28.         System.out.println("The average is " + avg);
  29.         in.close();
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement