我通过控制台工作的一个银行程序无限循环。我想在用户输入密码时隐藏密码。Console.readPassword()创建Java中
我创造了我自己的方法来读取密码,并返回使用readPassword()方法在控制台类的字符串:
/**
* Reads Password without allowing the console to show the characters
while the user is typing the password.
* @return
*/
public String readPass(){
String pass = "";
Console cnsl = System.console();
if(cnsl!=null){
char[] pword = cnsl.readPassword();
for(char c: pword){
pass+=c;
}
return pass;
}
return "";
}
的方法是从另一个类叫做:
/**
* Asks the user to enter their password. Then checks if the password is right.
* @param u
*/
public void checkPassword(User u){
boolean pa = false;
do{
System.out.print("Please enter your password: ");
//String pword = in.next();
String pword = b.readPass();
pa = u.checkPassword(pword);
if(pa){
System.out.println("\nLogin Successful...");
System.out.println("\nWelcome "+ u.getName() + "!");
enterAccount(u);
}
else{
System.out.println("That is the incorrect password. Please try again.\n");
}
}while(!pa);
}
在提示密码的那一刻,执行else块并且结果是无限循环。
@AhsN内,他为什么要这么做?它将作为循环中的第一个语句执行。 – Seelenvirtuose
@SpecialK也许你的方法'User.checkPassword(String)'总是返回'false'?另外,如果你没有控制台会发生什么?你将不会有机会输入任何东西,但是你的'readPass'方法只是返回一个空字符串(总是)。也许这是你的无限循环? – Seelenvirtuose
你确定checkPassword正常吗? – zubergu