【问题标题】:How to make Password an asterisk in java如何在java中使密码成为星号
【发布时间】:2014-10-05 01:25:50
【问题描述】:

我想使用 USER/user 作为我的项目的用户名和密码,但我不知道如何使密码显示为 * 或 • 键入时..

这是我的代码

String username,password;
int x=0;

do{
 System.out.print("Enter Username: ");
 username=br.readLine();
 System.out.print("Enter Password: "); 
 password=br.readLine();     
 System.out.print("  ");        

 if (username.equalsIgnoreCase("user")&&password.equalsIgnoreCase("user"))
 {x=x+2;}

else
 { System.out.println("     ");         
    System.out.println("WRONG USERNAME OR PASSWORD");}  
  System.out.println("     ");  

我希望输出是:

Enter Username: user
Enter Password: ••••

【问题讨论】:

标签: java string passwords int


【解决方案1】:

如果您想从控制台读取星号中的密码而不是尝试此操作,这仅适用于控制台 -

 Console console = System.console();

 username = console.readLine("Enter Username: ");

 char[] passwordArray = console.readPassword("Enter Password: ");

 password =  new String(passwordArray);

还有你的其他逻辑。

【讨论】:

  • 这不会按照 OP 的要求打印星号;它只是禁用“回声”
猜你喜欢
  • 2012-06-15
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 2018-10-05
  • 2012-05-19
  • 2012-08-09
相关资源
最近更新 更多