-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathMeanMedian2.java
64 lines (62 loc) · 1.61 KB
/
MeanMedian2.java
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
import javax.swing.*;
class MeanMedian2
{
public static void main(String[] args)
{
int[] nums = new int[20];
int num = 0;
String entry;
String list = "You entered: ";
int a, b;
int temp;
double median;
int total = 0;
double mean = 0;
final int QUIT = 9999;
int size = 0;
for(a = 0; num != QUIT && a < nums.length; ++a)
{
entry = JOptionPane.showInputDialog(null,
"Enter number " + (a + 1) + " or " + QUIT + " to quit");
num = Integer.parseInt(entry);
if(num != QUIT)
{
nums[a] = num;
total += nums[a];
if(a == 0)
list += nums[a];
else
list += ", " + nums[a];
}
}
System.out.println("size is " + a);
if(num != QUIT)
size = nums.length;
else
size = a - 1;
System.out.println("size is " + size);
System.out.println(list);
int comparisonsToMake = size - 1;
for(a = 0; a < size - 1; ++a)
{
for(b = 0; b < comparisonsToMake; ++b)
{
if(nums[b] > nums[b + 1])
{
temp = nums[b];
nums[b] = nums[b + 1];
nums[b + 1] = temp;
}
}
--comparisonsToMake;
}
if(size % 2 == 0)
median = (nums[(size / 2)] + nums[size / 2 - 1]) / 2.0;
else
median = nums[size / 2] / 1.0;
if(size != 0)
mean = total * 1.0 / size;
System.out.println("The mean is " + mean +
" and the median is " + median);
}
}