package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("What is the password?");
Scanner new2 = new Scanner(System.in);
int input = 0;
while(input <= 5 )
{
String password = new2.nextLine();
if(!password.equals("bluesky123")){
System.out.println("Incorrect password");
input++;
}
else if("bluesky123".equals(password)) {
System.out.println("You got it right!");
break;
}
else if(input == 5) {
System.out.println("maximum number of attempts reached");
break;
}
}
}
}
basically, once I hit the 5 loops, it just says "incorrect password" and breaks. not the "maximum attempts" message.
println("What is the password?")to be inside the loop?input == 5. You cover all cases with the first 2 blocks, and with either exit on the next loop whereinputis 5 or more (enter!password.equalsraises the variable to 5), or break because you got the right password.input <= 5) is false. Your code doesn't work because if!password.equals("bluesky123")is false and firstifstatement skips, then"bluesky123".equals(password)is definitely true, so there is no way for secondifstatement to skip, ergo there is no path thru the code that you ever allowelse if(input == 5)to execute.input == 5to the first case, and have the String verifications come after.