Results 1 to 3 of 3
  1. #1
    Joshsmith is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Need help - I can't figure it out.

    Java Code:
    boolean correct = false;
    
    do{ 
       numTextBook = Integer.parseInt(JOptionPane.showInputDialog("How many text books are you buying?"));
       if(numTextBook >= 0 && numTextBook <= 6)
          correct = true;
    } while (numTextBook >= 0 && numTextBook <= 6);
    I'm not sure what to do with the while to make this code work.

    Any help would be appreciated!
    Last edited by Fubarable; 10-23-2009 at 10:11 PM. Reason: Code tags added to aid code readability

  2. #2
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    boolean correct = false;
    
    do{ 
    
        numTextBook = Integer.parseInt(JOptionPane.showInputDialog("How many text books are you buying?"));
    
        if(numTextBook >= 0 && numTextBook <= 6)
            correct = true;
        }
    
    while (numTextBook >= 0 && numTextBook <= 6);
    What do you want it to do? Right now it's going to go through the DO at least once, and if numTextBook is between 0 - 6 it's going to set correct = true. and if it's true it's going to keep going in the loop indefinitely.

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

    Default

    You've got a boolean variable, correct, why not use it in the while condition? Which would work better do you think,

    Java Code:
    while (correct); // repeat while correct input given
    or
    Java Code:
    while (!correct); // repeat while correct not yet given

Similar Threads

  1. Need Help in BlueJ... Cannot figure this out.
    By ERICAMAUVE4 in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 12:34 AM
  2. I couldn’t figure out how to do this please help!
    By afrttoh in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 08:21 PM
  3. Replies: 2
    Last Post: 11-08-2008, 04:58 AM
  4. Replies: 2
    Last Post: 07-11-2008, 09:41 AM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

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