Please help in Java We will simulate a dice game in which 2
Please help in Java:
We will simulate a dice game in which 2 dice are thrown.
If the roll is 7 or 11, you win. If the roll is 2, 3 or 12, you lose. If the roll is any other value, it establishes a point. If, with a point established, that point is rolled again before a 7, you win. If, with a point established, a 7 is rolled before the point is rolled again you lose.
Build your algorithm incrementally. First write a program that simulates a roll of two dice, then outputs if it is a simple win (7 or 11), or a simple loss (2 or 3 or 12), or something else. Now fix the part where we wrote “something else”. In this case we rolled a number which we called total. We need to keep rolling the dice and looking at the new total each time. If the new total is the same as total, we win. If the new total is 7, we lose. And if something else we roll again…
This is what I have so far:
public static void main(String[] args) {
Random randGen = new Random ();
int d1, d2, d3, d4;
int total = 0;
int total2 = 0;
//get a raondom number between 1 and 6, called d1
d1 = randGen.nextInt(7) + 1;
//get a second random number between 1 and 6, called d2
d2 = randGen.nextInt(7) + 1;
//get the total and print it so we know what it is
total = d1 + d2;
System.out.println(\"First total: \"+ total);
//if the total is 7 or 11 print you win
if ((total == 7) || (total == 11)) {
System.out.println(\"You win.\");
}
//if the total is 2 or 3 or 12 you lose
else if ((total == 2) || (total == 3) || (total == 12)) {
System.out.println(\"You lose.\");
}
//part 2
do {
d3 = randGen.nextInt(7) + 1;
d4 = randGen.nextInt(7) + 1;
total2 = d3 + d4;
System.out.println(\"The new total is: \"+total2);
}
while ((total != total2) && (total2 != 7));{
if (total == total2)
System.out.println(\"You win.\");
if (total2 == 7)
System.out.println(\"You lose.\");
}
}
}
Solution
Hi, Please find my code.
I have arranged the given code in required format.
import java.util.Random;
public class DiceSimulation {
public static void main(String[] args) {
Random randGen = new Random ();
int d1, d2, d3, d4;
int total = 0;
int total2 = 0;
//get a raondom number between 1 and 6, called d1
d1 = randGen.nextInt(7) + 1;
//get a second random number between 1 and 6, called d2
d2 = randGen.nextInt(7) + 1;
//get the total and print it so we know what it is
total = d1 + d2;
System.out.println(\"First total: \"+ total);
//if the total is 7 or 11 print you win
if ((total == 7) || (total == 11)) {
System.out.println(\"You win.\");
}
//if the total is 2 or 3 or 12 you lose
else if ((total == 2) || (total == 3) || (total == 12)) {
System.out.println(\"You lose.\");
}
else{
do {
d3 = randGen.nextInt(7) + 1;
d4 = randGen.nextInt(7) + 1;
total2 = d3 + d4;
System.out.println(\"The new total is: \"+total2);
}while ((total != total2) && (total2 != 7));
if (total == total2)
System.out.println(\"You win.\");
else // total2 = 7
System.out.println(\"You lose.\");
}
}
}
/*
Sample run:
First total: 5
The new total is: 12
The new total is: 10
The new total is: 10
The new total is: 8
The new total is: 13
The new total is: 3
The new total is: 9
The new total is: 10
The new total is: 4
The new total is: 4
The new total is: 10
The new total is: 6
The new total is: 8
The new total is: 6
The new total is: 10
The new total is: 6
The new total is: 5
You win.
First total: 6
The new total is: 7
You lose.
*/

