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 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    1. What is the purpose of while (!validBet) in this code?

    Code:
    import javax.swing.JOptionPane;
    
    public class BettingQuestion {
    
    public static void main (String [] args) {
    
    int bank = 1000;
    int bet;
    boolean validBet;
    
    JOptionPane.showMessageDialog(null, "You can bet between 1 and " + bank);	
    do {
    bet = Integer.parseInt(
    JOptionPane.showInputDialog(null, "Enter your bet:"));
    validBet = true;
    if (bet <= 0 || bet >1000) {
    validBet = false;
    JOptionPane.showMessageDialog(null, "What, are you crazy?");
    }
    } while (!validBet);	
    
    JOptionPane.showMessageDialog(null, "Your money's good here.");
    }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    ! evaluates to NOT. So in this example, do/while validBet is false.


  •  

    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
    •