This is my code.
import java.util.Scanner;
public class passwordProgram {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String correctPassword = "WooHoo";
int tries = 0;
boolean keepGoing = true;
while(keepGoing = true) {
tries = tries + 1;
System.out.println("try #" + tries);
String password;
System.out.println("Please enter the password: ");
password = scan.next();
if(password == correctPassword) {
System.out.println("This is the correct result:" + password);
keepGoing = false;
if(tries >= 3) {
System.out.println("Too many wrong tries. Exiting program");
keepGoing = false;
break;
}
}
}
}
}
The while loop doesn't end when the right password is entered, and it keeps repeating after the allowed number of attempts has been reached and I want to know why.
Is it because of the condition statement in the while loop or is there something else wrong with the code?