Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a program to roll two dice ( (int)(Math.random() * 6 + 1) yields random numbers 1 - 6). Keep rolling until the two dice are equal. Then print the number of tries it took to roll two of the same dice.
- */
- public class WhileLoop4{
- public static void main(String[] args){
- int d1 = (int)(Math.random() * 6 + 1);
- int d2 = (int)(Math.random() * 6 + 1);
- int rolls = 1;
- while( d1 != d2 ){
- d1 = (int)(Math.random() * 6 + 1);
- d2 = (int)(Math.random() * 6 + 1);
- rolls++;
- }
- System.out.println("It took " + rolls + " rolls to get a double");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement