-4
  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

  • 2
    It's so simple, I suggest reading some (a lot of) Java [tutorials](https://stackoverflow.com/questions/721604/programming-with-java-for-beginners) and try to solve it yourself before asking. – user202729 Feb 06 '18 at 12:41
  • 5
    [How much effort is expected of StackOverflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – user202729 Feb 06 '18 at 12:41
  • @user202729 thanksfor a link ;-) – Betlista Feb 06 '18 at 12:54

1 Answers1

0

Not sure what randomNumers is but this might do the trick:

number1 = randomNumbers.nextInt(10) + 1;

  while (counter1 <= 9)
  {
     System.out.println("random number in while iteration " + counter1 + " equals: " + number1);

     randomTotal += number1;
     counter1++;
     if (number1 == 5)
        break;
     number1 = randomNumbers.nextInt(10) + 1;
 }

So you can put System.out.println wherever You like. And it always used same number, because you assigned variable only once and used it every loop iteration instead of changing it each loop iteration.

MrHutnik
  • 118
  • 3
  • 9
  • I suggest not answering such bad questions (bad, as evidenced by the vote tally) because it encourages bad questions to the site. – user202729 Feb 06 '18 at 12:59
  • Seems fair I guess. But I thought It's better to give an answer and so he actually receives an answer and perhaps learns something. Do You think I should delete the answer? – MrHutnik Feb 06 '18 at 13:04
  • 1
    [Reading meta](https://meta.stackoverflow.com/questions/281793/stance-on-answering-bad-questions), wait a minute. – user202729 Feb 06 '18 at 13:06
  • As I don't think the answer is bad(I wrote it so obviously I don't :D) the top answer from the topic you linked suggests editing the question, making it good. Not sure how to do that though. – MrHutnik Feb 06 '18 at 13:14