mardi 6 février 2018

How to generate a new number for each loops?

  System.out.println("Problem 11:");
  int randomTotal = 0;
  int number1;
  int counter1 = 0;

  number1 = randomNumbers.nextInt(10) + 1;

  while (counter1 <= 9)
  {
     randomTotal += number1;
     counter1++;
     if (number1 == 5)
        break;
   }
  System.out.println(randomTotal);

When the problem loops it reuses the same random number, how do I generate a new random number got each loop? I am trying to add 10 random numbers together and when the random number is 5 I want to create a break and stop the numbers it already has. So if it is 6, 7, 8, 5, it will add those together and not add any more numbers. Also is there a way to display what the random numbers are? Thank you




Aucun commentaire:

Enregistrer un commentaire