forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiceRoller.java
71 lines (56 loc) · 1.93 KB
/
DiceRoller.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
71
package diceroller;
/*
* DiceRoller takes a number of dice and the number of sides
* on each dice, "rolls" them, and displays them in an array.
*
* To use this program, enter the numbers in the console when
* prompted to do so.
*/
import java.io.*;
import java.util.Random;
class DiceRoller {
// Get number of sides on each die.
private static int getDiceSides() {
int sides = 0;
System.out.print("How many sides are on the dice? ");
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
sides = Integer.parseInt(br.readLine());
} catch(IOException exc) {
System.out.println("I/OException: " + exc);
}
return sides;
}
// Get total number of dice to roll.
private static int getNumberOfDice() {
int dice = 0;
System.out.print("How many dice to roll? ");
try(BufferedReader br = new BufferedReader(
new InputStreamReader(System.in))) {
dice = Integer.parseInt(br.readLine());
} catch(IOException exc) {
System.out.println("I/OException: " + exc);
}
return dice;
}
// Roll total number of dice within range of sides.
private static int[] rollDice(int sides, int dice) {
int[] numbers = new int[dice];
Random rand = new Random();
for(int i=0; i < dice; i++) {
numbers[i] = rand.nextInt(sides);
}
return numbers;
}
// Display output from rolling dice given sides and number of dice.
public static void main(String args[]) {
int sides = getDiceSides();
int dice = getNumberOfDice();
int[] numbers = rollDice(sides, dice);
System.out.print("Rolls: ");
for(int n: numbers) {
System.out.print(n + " ");
}
}
}