Two dice will be rolled and 2 random numbers between 1 and 6 will be generated. The sum will be taken from the 2 numbers and used to decide what is next. If user's sum is 3, 5, 7, 9,11 then they lose. If the sum is 4, 6, 8, 10 then they win. If sum is 2, 12 then the program automatically rolls the dice again until the user wins or loses. 1. Create a game for the above algorithm with following constructs in it: 2. Game should ask the user to input his/ her name. 3. Game should be able to generate random numbers between 1 - 6. 4. Program should be able to show 3 alternate paths a. Win : when the random number is in 4 , 6, 8, 10 b. Loose: 3,5,7,9,11 c. Play again: 2, 12
Basically I need keep getting a new random number if I get sum of 2 or 12 from 2 dice and that has to go into the loop again and again
public class assessmentrollingdicedemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter User Name: ");
String userName = sc.next();
Random r = new Random();
int firstRoll = r.nextInt(6);
int secondRoll = r.nextInt(6);
firstRoll+= 1;
secondRoll+= 1;
int timesWin = 0;
int timesLose = 0;
int sumOfdice = firstRoll + secondRoll;
do
{
System.out.println("Die 1: "+ firstRoll);
System.out.println("Die 2: "+ secondRoll);
System.out.println("total: "+ sumOfdice);
if(sumOfdice == 4 || sumOfdice == 6 || sumOfdice == 8 || sumOfdice == 10)
{
System.out.println("Win");
timesWin++;
}
if(sumOfdice == 3 || sumOfdice == 5 || sumOfdice == 7 || sumOfdice == 9 || sumOfdice == 11)
{
System.out.println("Lose");
timesLose++;
}
else
{
System.out.println("Play Again");
}
} while(sumOfdice == 12 || sumOfdice ==2);
System.out.println("You won " + timesWin + "times");
System.out.println("You lost " + timesLose + "times");
}
}
What I expected was rolling again should only applies to when sum of dice is 2 or 12 but I get infinite loops. Please help me if you know the answer.
Aucun commentaire:
Enregistrer un commentaire