Validating Input from the User with do-while loops - Java Language Basics

Java examples for Language Basics:while

Description

Validating Input from the User with do-while loops

Demo Code

import java.util.Scanner;

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

   public static void main(String[] args) {
      int bank = 1000;   // assume the user has $1,000
      int bet;           // the bet entered by the user

      System.out.println("You can bet between 1 and " +
          bank);//from  w  ww  .  j a v a2s .  co m
      do
      {
          System.out.print("Enter your bet: ");
          bet = sc.nextInt();
      } while ( (bet <= 0) || (bet > bank) );
      System.out.println("Your money's good here.");
   }
}

Related Tutorials