Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Ex04 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int y; // grade entered by the user
- int c = 0; // count valid grades
- int sum = 0;
- double avg;
- // get first grade
- System.out.print("Enter your grade (0 to 100): ");
- y = in.nextInt();
- while (y >= 0 && y <= 100) {
- // valid grade
- c++; // add 1 to the counter
- sum = sum + y; // add grade to sum
- // get next grade
- System.out.print("Enter your grade (0 to 100): ");
- y = in.nextInt();
- }
- System.out.println("You have " + c + " grades");
- System.out.println("The sum of grades is " + sum);
- avg = (double) sum / c;
- System.out.println("The average is " + avg);
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement