Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problems with my do-while loop?

    Code:
    import javax.swing.JOptionPane;
    public class Main {
    
    public static void main (String [] args) { 
    
    int bank = 1000;
    int bet; 
    
    JOptionPane.showMessageDialog(null, "You can bet between 1 and " + bank);
    
    do
    {
       bet =  Integer.parseInt(JOptionPane.showInputDialog(null, "Enter your bet:"));
        
    } while  (bet <= 0)|| (bet > bank);
        JOptionPane.showMessageDialog(null, "Your money's good here");
    }
    }
    Also, what does a Scanner class have anything to do with it?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Your condition is invalid. Fix the brackets around them.
    Scanner has nothing to do here, but that doesn't matter since there is no reference to the Scanner in this code.
    If you're using the ATOI from the integer.parseint, you should be using a try/catch on it.

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    while  (bet <= 0)|| (bet > bank);
    to

    Code:
    while  (bet <= 0 || bet > bank);


  •  

    Tags for this Thread

    Posting Permissions

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