利用扫描仪实现Java中的口令屏蔽 [英] Password Masking in Java with Scanner
本文介绍了利用扫描仪实现Java中的口令屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名学习Java的高中生,我想知道如何在Scanner中自动将输入文本更改为星号。这是我为一个项目做的一个简单的登录系统。我的代码是
Scanner scan = new Scanner(System.in);
boolean correctLogin = false;
String username;
String password;
String enteredUsername;
String enteredPassword;
while(correctLogin != true){
System.out.println("Enter Username: ");
enteredUsername = scan.nextLine();
System.out.println("Enter Password: ");
enteredPassword = scan.nextLine();
if(enteredUsername.equals("username") && enteredPassword.equals("passw00rd")){
System.out.println("You have entered the correct login info");
correctLogin = true;
break;
}
else{
System.out.println("Your login info was incorrect, please try again");
}
}
System.out.println("You are now logged in, good job!");
我想要它,以便在我键入密码时,它会自动变为星号。
推荐答案
尝试此密码读取:
Console console = System.console();
if(console != null){
console.readPassword("Enter Password: ");
}
这篇关于利用扫描仪实现Java中的口令屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文