JAVA
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
public class SnakeEyes
{
/* Roll a pair of dice until snake eyes (total value of 2) comes up */
public static void main(String[] args)
{
Dice dice = new Dice(); // pair of dice
int rollCount = 0; // Number of rolls
do {
dice.roll();
System.out.println(dice.getDice1() + " and " + dice.getDice2());
rollCount++;
}
while (dice.getTotal() != 2);
System.out.println("It took " + rollCount + " rolls to get snake eyes.");
}
}
class Dice
{
/* An object of class Dice represents a pair of dice, where each dice shows a number between 1 and 6. */
int dice1;
int dice2;
void roll() {
dice1 = (int)(Math.random()*6) + 1;
dice2 = (int)(Math.random()*6) + 1;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Ejecutar