问题是变量“check”和“password”都是相同的字符串,但if语句由于某种原因说它是假的。我该如何解决这个问题?
以下是代码:
import java.util.Scanner;
public class Manager {
public static Scanner input = new Scanner(System.in);
public static boolean isPasswordSet;
public static String password;
public static void password() {
String check;
while (true) {
if (isPasswordSet == false) {
System.out.println("Please set a new password: ");
password = input.next();
System.out.println("Enter password again: ");
check = input.next();
if (check == password) {
isPasswordSet = true;
System.out.println("Welcome");
return;
} else if (check != password) {
System.out.println("Password incorrect.");
isPasswordSet = false;
continue;
}
} else if (isPasswordSet == true) {
System.out.println("Please enter password: ");
check = input.next();
if (check == password) {
System.out.println("Welcome");
} else if (check != password) {
System.out.println("Password incorrect.");
continue;
}
}
}
}
public static void main(String args[]) {
password();
}
}
输出:
Please set a new password:
ryan
Enter password again:
ryan
Password incorrect.
Please set a new password: