gpt4 book ai didi

java - 使用 Java 扫描器进行密码屏蔽

转载 作者:行者123 更新时间:2023-12-01 06:08:58 24 4
gpt4 key购买 nike

我是一名正在学习 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 - 使用 Java 扫描器进行密码屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com