Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Nest Loop / While loop  RSS feed

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me fix this part of my code up? What I'm trying to do is let the "for" loop run depending on the amount the user enter. Each time the for loop run ,there would be a random number generated. The inner loop need to run until the the random number is equal to 1 . Each time that the inner loop to check if "randNum1 != 1" , "bottleCapNum" is +1 .



The problem with this code is the "randNum1 in while(randNum1 !=1) isn't initialized so I can't compile. Thank you for any suggestions.
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must declare and initialize randNum1 before the while loop. How can the program evaluate (randNum1 != 1) if it the variable doesn't exist? You can just initialize it to 0 so it will always pass the first time. You can still assign it a new random number each time through with:


A do..while loop may make more sense in this situation.

You need to review your use of curly brackets and indentation, I can't figure out what you are intending to do.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i came up with, you help me figure out why the for loop only run once even though I try to input like other number? Thanks
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some code that accomplishes something like you were trying to do, I think. Of course, wins is always going to be equal to counter. I'm not sure if you had an InputStreamReader or not already so I put that in.
You can also try handling the NumberFormatException if the user enters something other than an int.

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Your code is far above my level right now. May i ask how did the program still manage to look for "1" even though you didn't use any conditional statement? Thank you

Is it because since the code below is part of the outer loop so it will not execute until the inner become false?



 
Steve Myers
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Wow! Your code is far above my level right now. May i ask how did the program still manage to look for "1" even though you didn't use any conditional statement? Thank you

Is it because since the code below is part of the outer loop so it will not execute until the inner become false?





Exactly, there is no need to check the value of randNum1 again since we already know that it must be equal to 1 because the inner loop stopped.
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed intialization of randNum1 to randNum.nextInt(5) otherwise we get a skewed result.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!