Results 1 to 4 of 4

Thread: Loop and retry.

  1. #1
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Question 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    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?)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Retry constructing a thread/ terminate thread
    By coyote1982 in forum New To Java
    Replies: 6
    Last Post: 09-17-2009, 09:38 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. For Sale - Sun Java Certification Voucher with Free Retry
    By johnenom in forum Java Certification
    Replies: 0
    Last Post: 02-12-2009, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •