1

I'm wondering how I could get my dice rolling simulator to stop when it your total roll is one of 2 certain numbers, (example: rolling will stop when you reach a total roll of 7 or 12). Here is my code so far:

public class RollDice {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 int dice1; // First Dice.
 int dice2; // Second Dice.
 int total; // Sum of the two rolls. 
 dice1 = (int)(Math.random()*6) + 1;
 dice2 = (int)(Math.random()*6) + 1;
 total = dice1 + dice2;
 System.out.println("Your first roll is " + dice1);
 System.out.println("Your second roll is " + dice2);
 System.out.println("Your complete roll is " + total);
 } 
} 
Ryan
2,0942 gold badges17 silver badges33 bronze badges
asked May 12, 2015 at 22:28
0

1 Answer 1

3

Just wrap everything with a do-while loop:

int total; // Sum of the two rolls. 
do {
 int dice1; // First Dice.
 int dice2; // Second Dice.
 dice1 = (int)(Math.random()*6) + 1;
 dice2 = (int)(Math.random()*6) + 1;
 total = dice1 + dice2;
 System.out.println("Your first roll is " + dice1);
 System.out.println("Your second roll is " + dice2);
 System.out.println("Your complete roll is " + total);
} while (total != 7 && total != 12);

Or something to that extent. :)

See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

answered May 12, 2015 at 22:34
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason it won't let me resolve total as a variable in the last line, why is this?
Ah right, sorry about that, typing too fast. Answer edited.. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.