Repository navigation
Expand file tree
/
Copy pathLesson30_diceRollerProgram.java
More file actions
106 lines (95 loc) · 3.12 KB
/
Copy pathLesson30_diceRollerProgram.java
File metadata and controls
106 lines (95 loc) · 3.12 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
106
import java.util.Random;
import java.util.Scanner;
public class Lesson30_diceRollerProgram {
static Scanner sc = new Scanner(System.in);
static Random random = new Random();
public static void main(String[] args) {
System.out.println("Welcome to the Dice Roller Program!");
System.out.println("------------------------------------");
int numOfDice = getNumOfDice();
int total = getTotal(numOfDice);
System.out.println("The total is: " + total);
System.out.println("End of Program!");
sc.close();
}
static int getNumOfDice() {
System.out.print("Enter a number of dice you want to roll: ");
int numOfDice = Integer.parseInt(sc.nextLine());
while(numOfDice < 0) {
System.out.println("Enter an appropriate number of dice you want to roll: ");
numOfDice = Integer.parseInt(sc.nextLine());
}
return numOfDice;
}
static int getTotal(int numOfDice) {
int total = 0;
for(int i = 0; i < numOfDice; i++) {
int dice = random.nextInt(1, 6 + 1);
total += dice;
displayDice(dice);
}
return total;
}
static void displayDice(int dice) {
System.out.println();
switch(dice) {
case 1: {
System.out.println(
"---------\n" +
"| |\n" +
"| o |\n" +
"| |\n" +
"---------");
break;
}
case 2: {
System.out.println(
"---------\n" +
"| o |\n" +
"| |\n" +
"| o |\n" +
"---------");
break;
}
case 3: {
System.out.println(
"---------\n" +
"| o |\n" +
"| o |\n" +
"| o |\n" +
"---------");
break;
}
case 4: {
System.out.println(
"---------\n" +
"| o o |\n" +
"| |\n" +
"| o o |\n" +
"---------");
break;
}
case 5: {
System.out.println(
"---------\n" +
"| o o |\n" +
"| o |\n" +
"| o o |\n" +
"---------");
break;
}
case 6: {
System.out.println(
"---------\n" +
"| o o |\n" +
"| o o |\n" +
"| o o |\n" +
"---------");
break;
}
}
System.out.println();
System.out.println("*******************");
System.out.println();
}
}