Use a boolean variable that's set in the body of the do-while loop if the data is invalid - Java Language Basics

Java examples for Language Basics:while

Description

Use a boolean variable that's set in the body of the do-while loop if the data is invalid

Demo Code

import java.util.Scanner;

public class GetABet3{
   static Scanner sc = new Scanner(System.in);

   public static void main(String[] args) {
      int bank = 1000;   
      int bet;           
      boolean validBet;  
      System.out.println ("You can bet between 1 and " + bank);
      do {/*from   ww w.  j a  v a  2s .  c  o m*/
          System.out.print("Enter your bet: ");
          bet = sc.nextInt();
          validBet = true;
         
          if ( (bet <= 0) || (bet > bank) ){
              validBet = false;
              System.out.println("wrong.");
          }
      } while (!validBet);

   }
}

Related Tutorials