-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNrDiff.java
More file actions
24 lines (16 loc) · 794 Bytes
/
NrDiff.java
File metadata and controls
24 lines (16 loc) · 794 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
public class NrDiff {
public static void main(String[] args) {
int[] numbers = {1, 5, 9, 15};
// we create a more robust way check first values of array, in this case both are set to 1
// now it does not matter what the start value of array is, previous logic assumed the next value in array always increased
int min = numbers[0];
int max = numbers[0];
// enhanced for-loop, we use num which checks each value of the array and assigns what it finds to min or max (depending on < or >)
for (int num : numbers) {
if (num < min) min = num;
if (num > max) max = num;
}
// print out the difference between max and min values
System.out.println("Difference = " + (max - min));
}
}