在C#中,可以使用Console.ReadKey方法来隐藏用户输入的内容。具体代码如下所示:
using System;class Program{static void Main(){Console.Write("请输入密码:");string password = "";ConsoleKeyInfo key;do{key = Console.ReadKey(true);if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter){password += key.KeyChar;Console.Write("*");}else{if (key.Key == ConsoleKey.Backspace && password.Length > 0){password = password.Substring(0, (password.Length - 1));Console.Write("\b \b");}}}while (key.Key != ConsoleKey.Enter);Console.WriteLine();Console.WriteLine("您输入的密码是:" + password);}}上面的代码中,用户输入密码时,控制台会显示星号(*)代替用户输入的字符,从而隐藏密码内容。
上一篇:C++模板特化指的是什么意思
下一篇:java中console如何调出来
C









