-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise4.java
More file actions
28 lines (25 loc) · 881 Bytes
/
Copy pathExercise4.java
File metadata and controls
28 lines (25 loc) · 881 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
import java.util.ArrayList;
import java.util.Random;
/**
* 6.8.4. Exercise 4
*
* Create an ArrayList of Integers named dice4. Generate an Integer representing
* a roll of a six-sided die 5 times, adding each result to dice4. Print the
* ArrayList using an enhanced for loop.
* Sample output: dice4 = 3 2 4 4 1
*/
public class Exercise4 {
public static void main(String[] args) {
ArrayList<Integer> dice4 = new ArrayList<>();
Random rand = new Random();
// Generate 5 random dice rolls and add to ArrayList
for (int i = 0; i < 5; i++) {
dice4.add(rand.nextInt(6) + 1); // Generate random value from 1 to 6
}
// Print ArrayList using enhanced for loop
System.out.print("dice4 = ");
for (int roll : dice4) {
System.out.print(roll + " ");
}
}
}