Number of tries | open | attempt < 3 | !open | attempt < 3 && !open | Another Chance? |
---|---|---|---|---|---|
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.util.Scanner; class LoopingLock { public static void main( String[] args ) { int lockFirst = 6, lockSecond = 12, lockThird = 30; int numb; Scanner scan = new Scanner( System.in ); 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.print("\nEnter first number: "); numb = scan.nextInt(); if ( numb != lockFirst ) correct = false ; //Second Number System.out.print("Enter second number: "); numb = scan.nextInt(); if ( numb != lockSecond ) correct = false ; //Third Number System.out.print("Enter third number: "); numb = scan.nextInt(); 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.
Can you fill in the blanks without getting messed up?