ElevatorSimulation2.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program simulates an elevator panel that skips the 13th floor, checking for 
  5     input errors.
  6  */
  7  public class ElevatorSimulation2
  8  {
  9     public static void main(String[] args)
 10     {  
 11        Scanner in = new Scanner(System.in);
 12        System.out.print("Floor: ");
 13        if (in.hasNextInt())
 14        {
 15           // Now we know that the user entered an integer
 16  
 17           int floor = in.nextInt();
 18  
 19           if (floor == 13)
 20           {
 21              System.out.println("Error: There is no thirteenth floor.");
 22           }
 23           else if (floor <= 0 || floor > 20)
 24           {
 25              System.out.println("Error: The floor must be between 1 and 20.");
 26           }
 27           else
 28           {
 29              // Now we know that the input is valid
 30  
 31              int actualFloor = floor;
 32              if (floor > 13)
 33              {  
 34                 actualFloor = floor - 1;
 35              }
 36  
 37              System.out.println("The elevator will travel to the actual floor "
 38                 + actualFloor);
 39           }
 40        }
 41        else
 42        {
 43           System.out.println("Error: Not an integer.");
 44        }
 45     }
 46  }