-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9Ex1.java
More file actions
38 lines (35 loc) · 820 Bytes
/
Ch9Ex1.java
File metadata and controls
38 lines (35 loc) · 820 Bytes
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
import java.util.stream.*;
import java.util.Scanner;
public class Ch9Ex1
{
public static void main(String[] args)
{
while (true)
{
Scanner input = new Scanner(System.in);
System.out.println("How many numbers would you like to average?");
int arrayLength = input.nextInt();
int[] intArray = new int[arrayLength];
for (int x=arrayLength; x>0 ;x--)
{
try
{
System.out.println("Enter a Number: ");
int uinput = input.nextInt();
if (uinput<0)
throw new RangeException();
intArray[x-1] = uinput;
}
catch(RangeException e)
{
x++;
System.out.println("N must be positive");
}
}
double sum = (IntStream.of(intArray).sum());
double avg = sum/arrayLength;
System.out.println("Your average is: "+ avg);
break;
}
}
}