Advertisement
fosterbl

WhileLoop4.java

Oct 8th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. /*
  2. 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.
  3. */
  4. public class WhileLoop4{
  5.    public static void main(String[] args){
  6.       int d1 = (int)(Math.random() * 6 + 1);
  7.       int d2 = (int)(Math.random() * 6 + 1);
  8.       int rolls = 1;
  9.      
  10.       while( d1 != d2 ){
  11.          d1 = (int)(Math.random() * 6 + 1);
  12.          d2 = (int)(Math.random() * 6 + 1);
  13.          rolls++;
  14.       }
  15.      
  16.       System.out.println("It took " + rolls + " rolls to get a double");
  17.    }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement