Advertisement
karlakmkj

while loop - random numbers

Dec 2nd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. // Importing the Random library
  2. import java.util.Random;
  3.  
  4. class LuckyFive {
  5.  
  6.   public static void main(String[] args) {
  7.    
  8.     // Creating a random number generator
  9.     Random randomGenerator = new Random();
  10.    
  11.     // Generate a number between 1 and 6
  12.     int dieRoll = randomGenerator.nextInt(6) + 1; //+1 so that this will not include 0 but the highest no. itself 6
  13.                                                   //without +1 -- will generate number from 0 to 5
  14.  
  15.     // Repeat while roll isn't 5
  16.     while (dieRoll !=5){
  17.       System.out.println(dieRoll);
  18.       dieRoll = randomGenerator.nextInt(6) + 1;
  19.    
  20.     /*this while loop will generate a different set of numbers each time of different length
  21.     because until it generates the number 5 then it will stop & exit the loop
  22.     */
  23.     }
  24.   }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement