Pages

Saturday, 20 August 2016

Chapter 7 Exercise 4, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

7.4 (Analyze scores) Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100.


import java.util.Scanner;
 
public class ProgrammingEx7_4 {
 
 public static void main(String[] args) {
  final int MAX_NUMBER_OF_SCORE = 100;
 
  Scanner input = new Scanner(System.in);
  int[] score = new int[MAX_NUMBER_OF_SCORE];
  int sum = 0, avg = 0, n = 0;
  System.out.print("Enter scores (End input with negative number):");
  for (int i = 0; i < score.length; i++) {
 
   score[i] = input.nextInt();
 
   if (score[i] < 0) {
    avg = sum / i;
    break;
   } else {
    sum += score[i];
    n++;
   }
  }
 
  int equal = 0, above = 0, below = 0;
  for (int i = 0; i < n; i++) {
   if (score[i] == avg) {
    equal++;
   } else if (score[i] > avg) {
    above++;
   } else {
    below++;
   }
 
  }
 
  System.out.println("The number of scores equal to average is " + equal);
  System.out.println("The number of scores above average is " + above);
  System.out.println("The number of scores below average is " + below);
 }
 
}

No comments:

Post a Comment