-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiceGame.java
70 lines (61 loc) · 1.34 KB
/
DiceGame.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
//# DiceyGames//
//Dice Game Mid-Project( Stella Lin & Nahima Abea)//
package midCourseProject;
public class DiceGame {
static Boolean keepRolling = true;
public static void main(String[] args) {
int point = 0;
point = rollDice();
System.out.println(firstTry(point));
int sum = 0;
while (keepRolling) {
sum=rollDice();
System.out.println(continueRolling(point,sum));
}
}
public static int rollDie() {
return 1 + (int)(Math.random() * 6);
}
public static int rollDice() {
int die1 = rollDie();
int die2 = rollDie();
int sum = die1 + die2;
System.out.println("You rolled "+sum+".");
return sum;
}
public static String firstTry(int point) {
String firstMessage;
switch(point) {
case 7:
case 11:
firstMessage = "You win!";
keepRolling = false;
return firstMessage;
case 2:
case 3:
case 12:
firstMessage = "You lose!";
keepRolling = false;
return firstMessage;
default:
firstMessage = "POINT is "+point+".";
return firstMessage;
}
}
public static String continueRolling(int point, int sum) {
String message;
if (sum == point) {
message = "You win!";
keepRolling = false;
return message;
} else if (sum == 7){
message = "You lose!";
keepRolling = false;
return message;
}
else {
message = "POINT is "+point+".";
return message;
}
}
}