Pasted as Java [Remove this snippet ]
Description: Week2Lab2 - Analyze Scores
URL: altepeter.com/misc/snippets/results/zV3ush38.html
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
/**
 * @author Jeremiah Altepeter
 *
 * Week 2 Lab 2
 * Gather a collection of scores from the user using an input box
 * 	display the average of all the scores in a dialog
 * 	display the number of scores above or equal to avg in a dialog
 * 	display the number of scores less then avg in a dialog 
 * 	
 */
package AnalyzeScores;
 
import javax.swing.JOptionPane;
 
public class AnalyzeScores {
 
	public static void main(String[] args) {
		// Array to hold max20 scores
		double[] scores = new double[20];
		// initialize variables
		double total = 0;
		String input = "";
		int count = 0;
		int above = 0;
 
		// allow user to exit with "cancel" button or "-1" input
		while(count<=20&&input!=null){
			// show the input dialog
			input = JOptionPane.showInputDialog("Enter score #"+(count+1)+
				" (Cancel or -1 to end)");
			if (input==null)
				continue; // cancel was pressed
			if(input.equals("-1")){
				System.out.println("negative");
				input = null;
				break;
			}
			System.out.println(input);
			// convert the input to double and store in array
			scores[count] = Double.parseDouble(input);
			// increase counter
			count++;
		}// end while
 
		// find the total of all scores
		for (int i = 0; i<count; i++)
			total = total+scores[i];
 
		// find how many are <= avg
		for(int i=0; i<count; i++)
			if(scores[i]>=(total/count)) above++;
 
		// generate output message
		String message = "Average of " + count + " scores: "+ (total/count) + "\n" +
			"The number of scores above or equal to the average: " + above + "\n" +
			"The number of scores below the average: "+ (count-above);
 
		// display the dialog
		if(count>0)
			JOptionPane.showMessageDialog(null, message);
 
	}// end main()
 
}// end class AnalyzeScores