Number of tries | open | attempt < 3 | !open | attempt < 3 && !open | Another Chance? |
---|---|---|---|---|---|
3 | false | false | true | false && true | false |
3 | true | false | false | false && false | false |
Here is the near-complete program,
created by "pasting" most of the previous program's main()
method into the loop body discussed above.
The indenting has been adjusted appropriately,
and a few other minor changes have been made.
import java.io.*; class LoopingLock { public static void main( String[] args ) throws IOException { int lockFirst = 6, lockSecond = 12, lockThird = 30; int numb; BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); String input; int attempt = 0; boolean open = false; while ( attempt < 3 && !open ) { // try a combination, setting open to "true" if correct boolean correct = _____________; //First Number System.out.println("\nEnter first number: "); input = stdin.readLine(); numb = Integer.parseInt( input ); if ( numb != lockFirst ) correct = false ; //Second Number System.out.println("Enter second number: "); input = stdin.readLine(); numb = Integer.parseInt( input ); if ( numb != lockSecond ) correct = false ; //Third Number System.out.println("Enter third number: "); input = stdin.readLine(); numb = Integer.parseInt( input ); if ( numb != lockThird ) correct = false ; //Result if ( correct ) { System.out.println("Lock opens"); open = _____________ } else System.out.println("Lock does not open"); attempt = attempt + 1; } } }
A few things remain to be patched up.
Luckily,
you can easily do them.
Pay attention to how the variables open
and correct
work with the loop.
It is very easy to get things like this messed up.