mardi 11 août 2020

Java, Loops, Random. Why does this print the number more than once?

I'd like to understand why this code prints the random numbers more than once, sometimes I run it and prints one number, sometimes it prints three or four at the same time, could I make it print just one random number?

`public static void main(String[] args) {

// Creating a random number generator
Random randomGenerator = new Random();

// Generate a number between 1 and 6
int dieRoll = randomGenerator.nextInt(6) + 1;

// Repeat while roll isn't 5
while (dieRoll != 5) {
  System.out.println(dieRoll);
  dieRoll = randomGenerator.nextInt(6) + 1;
}

}`




Aucun commentaire:

Enregistrer un commentaire