首页 > 解决方案 > 使用java中的while循环将用户输入转换为星号之类的密码

问题描述

在java中使用while循环如何在输入密码时将用户输入转换为星号。这是可能的,如何?谢谢。

        System.out.print("Enter password: ");

        while(true)
        {
            password = pal.nextLine();
            password = "*"; 
            System.out.print(password);
        }

这是我唯一的想法,但它不起作用

标签: java

解决方案


不确定是否显示星号,但您可以使用该Console.readPassword方法使密码不可见

public class Main {    

  public static void main(String[] args) {
    char passwordArray[] = System.console().readPassword("Enter password: ");
     // entered password will be stored in this array        
  }
}

推荐阅读