# Java Program That Repeatedly Throws Two Dices Until They Are Both Six

In this tutorial, we will create different solutions for a dice problem. We will throw 2 dices until both of them is 6, we will solve this problem with both for and while loops.

Solution 1: While loop solution

``````int counter = 0;
int dice1, dice2;
do
{
dice1 = (int)(Math.random()*6)+1;
dice2 = (int)(Math.random()*6)+1;
counter++;
System.out.println(counter + "th try: " + dice1+"-"+dice2);
}
while (!(dice1 == 6 && dice2 == 6));

System.out.println("Congratulations, you won on " + counter + "th try");``````

Console Output:

1th try: 4-2
2th try: 1-2
3th try: 4-1
4th try: 2-1
5th try: 5-4
6th try: 6-6
Congratulations, you won on 6th try

Solution 2: While loop with different condition

``````int counter = 0;
int dice1, dice2;
do
{
dice1 = (int)(Math.random()*6)+1;
dice2 = (int)(Math.random()*6)+1;
counter++;
System.out.println(counter + "th try: " + dice1+"-"+dice2);
}
while (dice1 + dice2 < 12);

System.out.println("Congratulations, you won on " + counter + "th try");``````

Console Output:

1th try: 5-2
2th try: 3-5
3th try: 1-1
4th try: 3-5
5th try: 6-6
Congratulations, you won on 5th try

Solution 3: We can also solve this problem with for loop and if statement.

``````int counter;
int dice1, dice2;
for (counter=1; counter>0; counter++) //infinite loop
{
dice1 = (int)(Math.random()*6)+1;
dice2 = (int)(Math.random()*6)+1;
System.out.println(counter + "th try: " + dice1+"-"+dice2);

if (dice1 + dice2 == 12)
break;
}

System.out.println("Congratulations, you won on " + counter + "th try");``````

Console Output:

1th try: 2-5
2th try: 6-5
3th try: 3-3
4th try: 3-2
5th try: 4-4
6th try: 4-3
7th try: 2-6
8th try: 6-6
Congratulations, you won on 8th try