-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractice.java
70 lines (60 loc) · 1.97 KB
/
Practice.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
65
66
67
68
69
70
/*
* Arianna Biernacki
* Student ID: xxxxxxxxx
*/
package Week4;
import java.util.ArrayList;
import java.util.Scanner;
public class Practice {
ArrayList arrList = new ArrayList();
public void play() {
getUserInput();
count();
sum();
max();
reverseSequence();
}
private void getUserInput() {
Scanner input = new Scanner(System.in);
System.out.println("Enter any number (-100 will break the loop):");
OUTER_LOOP:
while (true) {
for (int i = 0;; i++) {
arrList.add(input.nextInt());
if (arrList.get(i).equals(-100)) {
System.out.println("You entered -100... Exiting the loop.");
break OUTER_LOOP;
}
}
}
}
private void count() {
System.out.println("-----------------------------------------");
System.out.println("The amount of numbers in the array are: " + arrList.size());
}
private void sum() {
System.out.println("-----------------------------------------");
int sum = 0;
for (int i = 0; i < arrList.size(); i++) {
sum += (int) arrList.get(i);
}
System.out.println("The sum is: " + sum);
}
private void max() {
System.out.println("-----------------------------------------");
int max = 0;
for (int i = 0; i < arrList.size(); i++) {
if ((int) arrList.get(i) > max) {
max = (int) arrList.get(i);
}
}
System.out.println("The largest number in the array is: " + max);
}
private void reverseSequence() {
System.out.println("-----------------------------------------");
System.out.println("The array in reverse is: ");
for (int i = arrList.size() - 1; i > -1; i--){
System.out.println(arrList.get(i));
}
}
}