//****************************************************************************** // LockerCombo simulates a combination lock, accepting three integers as input // and opening the lock if the integers fall within a tolerance of plus-or-minus two // digits of the correct numbers, and within three attempts. // Kelli Wiseth // 19-July-2004 // Platform: Wintel [Windows 2000] // JDK version: JDK 1.4.1 // Time spent: ~ 6 hours // //****************************************************************************** import java.io.*; class LockerCombo { static final int NUM_1=34; static final int NUM_2=11; static final int NUM_3=23; public static void main (String args[]) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String combo_string; int comb_1; int comb_2; int comb_3; int num_tries=0; boolean state_flag=false; do { System.out.print("Please enter the lock combination as nn-nn-nn: "); System.out.flush(); combo_string=stdin.readLine(); comb_1=Integer.parseInt(combo_string.substring(0,2)); comb_2=Integer.parseInt(combo_string.substring(3,5)); comb_3=Integer.parseInt(combo_string.substring(6)); if ((comb_1>=NUM_1-2) && (comb_1<=NUM_1+2)) { if ((comb_2>=NUM_2-2) && (comb_2<=NUM_2+2)) if ((comb_3>=NUM_3-2) && (comb_3<=NUM_3+2)) state_flag=true; } else { System.out.println("Sorry. That's not the right combination."); } num_tries++; } while ((num_tries<3) && (!state_flag)); if (state_flag) System.out.println("The locker is OPEN"); else System.out.println("The locker is NOT OPEN and you can't try any more right now."); }//main method }//LockerCombo class