forked from ZeroAccess/CompleteJavaDevelopersCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoreExamples.java
More file actions
37 lines (30 loc) · 1013 Bytes
/
Copy pathMoreExamples.java
File metadata and controls
37 lines (30 loc) · 1013 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
package Arrays;
import java.util.Scanner;
/**
* Created by robertsg on 12/3/2015.
*/
public class MoreExamples {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[] myIntegers = getIntegers(5);
for (int i = 0; i < myIntegers.length; i++) {
System.out.println("Element " + i + ", typed value was " + myIntegers[i]);
}
System.out.println("The average is " + getAverage(myIntegers));
}
public static int[] getIntegers(int number) {
System.out.println("Enter " + number + " integer values.\r");
int[] values = new int[number];
for (int i = 0; i < values.length; i++) {
values[i] = scanner.nextInt();
}
return values;
}
public static double getAverage(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return (double) sum / (double) array.length;
}
}