-
Loop and retry.
Hey Guys/Gals,
I'm attempting to build a loop that adds one to a counter each time a user answers a question correctly. Then once the set number of questions have been asked, the user is prompted to see if they want to go again.
I've got this all working correctly so far, here's where the problem comes in. To begin I needed to initialize the count variable in the first method count = 0;
When the user says yes to go again, I call the method with the loop, which sets the count to zero and away we go.
I guess my main problem is how can I make the counter outside of the loop?
Just as an added bonus problem, I'd also like to keep track of how many questions have been asked, but I assume that the same principal for putting the counter outside the loop would help with this also.
Thanks,
KaW
-
declare your counter variable(s) before the loop. Then increment them when necessary in the loop.
-
OK,
so what you're saying is:
Main Method
int count = 0
Loop Method
++count
Retry Method
yes/no
Loop()
Display Method
System.out.println(count);
Here's where I'm fuzzy. As long as I don't declare a local variable named count in any other method, then they will all change the value of the count variable in Main? (When I increment them that is?)
-
Are you using static methods? If so then how about moving into the OO world. Then you can make your variable an instance variable which all your methods (except main) have access to. Then in main create a instance of your class and get it to run by calling a method to drive the rest of the code.