我需要在以下代码中插入一个while循环,这样当用户输入错误的密码时,它会再次请求它,直到它是正确的密码。
import javax.swing.JOptionPane;
public class PasswordManager {
private static String masterPassword = "secret3";
public static void main(String[] args) {
boolean correctPassword = false;
while
String password = JOptionPane.showInputDialog(null, "Enter Password");
correctPassword = checkPassword(password);
if (correctPassword == true)
System.out.println("Correct Password! :)");
else
System.out.println("Incorrect Password :(");
}
private static boolean checkPassword(String password){
if(password.equalsIgnoreCase(masterPassword))
return true;
else
return false;
}
}
答案 0 :(得分:1)
我的口味是这样的简洁形式:
public static void main(String[] args) {
String password = JOptionPane.showInputDialog(null, "Enter Password");
while (! checkPassword(password)) {
password = JOptionPane.showInputDialog(null, "Incorrect Password :(\nEnter Password");
}
JOptionPane.showMessageDialog(null, "Correct Password! :)");
}
我删除了correctPassword
变量。