• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repeat a while loop  RSS feed

 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to get the while loop to repeat itself as many times as I use as the input in the matches variable. I know that I can put a while loop inside another while loop, but I can
t quite seem to figure out how to solve this by myself. If anyone would be willing to give me some guideline on how to do this it would be much appreciated!

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trying to get the while loop to repeat itself as many times as I use as the input in the matches variable

That sounds like a single while loop would work.
Can you explain what is wrong with the current code?
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently the loop runs untill it hits the target value of the dices 1 time, but with the matches variable the user should be able to input how many times the target outcome should happen. Say i want the program to roll 11, 3 times. Currently it stops when it hits 11 the first time, but i would like it to stop when it's done 3 times. So the while loop should repeat as many times as the user inputs in the matches value.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like it to stop when it's done 3 times.

If you know how many times the loop should iterate, use a for loop.  Use a while loop when you don't know in advance how many times to iterate.
Something like this:
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Norm's suggestions, I would also recommend de-cluttering the main method by pulling out the section of code which rolls the dice and putting it in a separate method.  For example, create a method called roll which takes an argument of the target number, and returns the number of rolls it took to match:


 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
i would like it to stop when it's done 3 times.

If you know how many times the loop should iterate, use a for loop.  Use a while loop when you don't know in advance how many times to iterate.
Something like this:


The matches value decides how many times it should iterate so it will change from time to time, it's not a pre deteremined value
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The matches value decides how many times it should iterate

If that is know before the for loop is entered, then replace the 3 used in the for statement with a variable that contains the number of times.

If the number of iterations is not known before the loop is entered, use a while loop.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, he doesn't want the loop to execute a predetermined number of times.  He wants the loop to continue to execute until something happens some number of times.  e.g. If matches=3 he want to keep looping until random3 == target three times, even if he has to loop 1000 times. 

The problem is with the if() condition inside the while() loop.  The if checks to see if the random number matches the target once.  It will set ferdig to true on the first match.  How could rework the if() statement to set ferdig to true only after the right number of matches?
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I managed to solve the problem I was having by putting the while loop inside another while statement.
Do you guys see any obvious things I could have done better with the code? Trying to learn as much as possible to not fall behind with the course i'm taking. We still haven't gotten to the point where we put a part of the code outside the main method so i'm trying to not implement too many techniques while i'm still on basic stages of coding.
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things that you could do:
  • define ferdig outside of the loop
  • define random3  outside of the loop

  • Giving you:
    Instead of:
    That way you are reusing the same variable and not redefining it.  This saves execution time, and memory.
    This may not make too much difference with this program, but it could in real world situations.
     
    Norm Radder
    Rancher
    Posts: 2240
    28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would suggest that line 28 be a for statement that uses  matches to control the number of iterations instead of while statement.


    I don't think variables should be defined outside of the scope where they are used making them more global and possibly prone to misuse.

    Let's see if what others think about where to define variables.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would get rid of the ferdig variable completely:

    Then you write the results after the loop exits.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried to think of a version with Streams. It is debatable if that is a beginners subject, but here goes:
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!