-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceRollProgram.java
More file actions
105 lines (95 loc) · 3.3 KB
/
DiceRollProgram.java
File metadata and controls
105 lines (95 loc) · 3.3 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.Random;
import java.util.Scanner;
public class DiceRollProgram {
static Scanner scanner = new Scanner(System.in);
static Random random = new Random();
public static void main(String[] args) {
// JAVA DICE ROLLER PROGRAM
// 1. Declare variables
int numberOfDice;
int total = 0;
// 2. Get user input (ask how many dice they want to roll)
do {
System.out.print("Enter the number of dice to roll: ");
numberOfDice = scanner.nextInt();
scanner.nextLine(); // Clear the buffer
if (numberOfDice > 6) {
System.out.println("INVALID NUMBER! Please enter a number between 1 and 6.");
}
else if (numberOfDice < 0) {
System.out.println("INVALID! Please enter a positive number (1-6).");
}
} while (numberOfDice > 6 || numberOfDice < 0);
// If you chose a number between 1 and 6, the dice program will continue
if (numberOfDice > 0 && numberOfDice <= 6) {
for (int i = 0; i < numberOfDice; i++) {
int roll = random.nextInt(1, 7); // Rolling a six-sided die
printDie(roll); // Display each die rolled
System.out.println("You rolled " + roll); // Display the number rolled
total += roll; // To calculate total, add each number you rolled
}
// Output the total
System.out.println();
System.out.println("***********************");
System.out.println("Your Total: " + total);
System.out.println("***********************");
}
scanner.close();
}
// 3. Display the dice - Here we will a method
static void printDie(int roll) {
// Display each die you roll
String dice1 = """
-------
| |
| ● |
| |
-------
""";
String dice2 = """
-------
| ● |
| |
| ● |
-------
""";
String dice3 = """
-------
| ● |
| ● |
| ● |
-------
""";
String dice4 = """
-------
| ● ● |
| |
| ● ● |
-------
""";
String dice5 = """
-------
| ● ● |
| ● |
| ● ● |
-------
""";
String dice6 = """
-------
| ● ● |
| ● ● |
| ● ● |
-------
""";
// Whatever roll is, display the appropriate die
switch(roll) {
case 1 -> System.out.print(dice1);
case 2 -> System.out.print(dice2);
case 3 -> System.out.print(dice3);
case 4 -> System.out.print(dice4);
case 5 -> System.out.print(dice5);
case 6 -> System.out.print(dice6);
default -> System.out.print("INVALID ROLL");
}
}
}