In Java, Analyze Scores Given By User

import java.util.Scanner;
public class AnalyzeScores{
    public static void main(String[]args){
        int num, sum = 0, cnt = 0;
        int[]nums=new int[100];
        Scanner in = new Scanner(System.in);
        System.out.print("Enter N numbers (-1 to quit): ");
        while(in.hasNextInt() && cnt < nums.length){
            num = in.nextInt();
            if(0 > num) break;
            nums[cnt++] = num;
            sum += num;
        }
        if(0 == cnt){
            System.out.println("Error: No inputs given!");
            return;
        }
        int avg = sum/cnt, abAvg = 0, blAvg = 0;
        for(int i = cnt-1; 0 <= i; i --)
            if(nums[i] >= avg) abAvg ++;
            else blAvg ++;
        System.out.printf("Nos# above avg: %d\n", abAvg);
        System.out.printf("Nos# below avg: %d\n", blAvg);
    }
}

Comments

Popular posts from this blog