PDA

View Full Version : Ask java questions online for free


dookey
Jun 20, 2013, 12:52 PM
The entry code for a room is 2 3 4. The user enters one digit at a time and may type in any number of wrong values, but as soon as the correct sequence is entered a „door open‟ message should be displayed. E.g. 2 5 2 3 4

my loop stops looping after user enters one right entry code but I want it to keep looping until user enters 3 right entry code


this is what I done so far


Scanner input = new Scanner (System.in);

int code1 = 2;
int code2 = 3;
int code3 = 4;
int code = 0;



for(int index = 0; code != code1 && code != code2 && code != code3; index++) {


System.out.println("Enter code");
code = input.nextInt();

if ((code == code1) && (code == code2) && (code == code3)) {
System.out.println("door open");
}

else if ((code != code1) && (code != code2) && (code != code3)) {
System.out.println("wrong code");
}

}