在*中显示密码字符 [英] To display password characters in *

查看:174
本文介绍了在*中显示密码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已经编写了代码以在*中显示我的密码字符,但它也是输入作为我的密码的一个字符。你可以帮帮我吗?

下面是我的代码:

Hi,

I have written the code to display my password characters in "*" but it is also consediring "enter" as one character for my password. Can you please help me with this?
Below is my code:

Console.Write("Enter Password :");
ConsoleKeyInfo key;String pwd="";
do
{
    key = Console.ReadKey(true);

    if (key.Key != ConsoleKey.Backspace)
    {
        pwd += key.KeyChar;
        Console.Write("*");                                    
    }

} while (key.Key != ConsoleKey.Enter);

pwd = Console.ReadLine();





请告诉我哪里出错了。

注意:

我没有任何方法或东西返回密码。



感谢advacne。



Please let me know where i am going wrong.
Note:
I dont have any method or something returning the password.

Thanks in advacne.

推荐答案

使用while循环代替do while ...它将解决你的问题
use the while loop instead of do while ... it will solve your problem


更改你的代码如下: br />
Change your code something like this:
Console.Write("Enter Password :");
ConsoleKeyInfo key; String pwd = "";
do
{
    key = Console.ReadKey(true);

    if (key.Key != ConsoleKey.Backspace && key.Key!=ConsoleKey.Enter)
    {
        pwd += key.KeyChar;
        Console.Write("*");
    }

} while (key.Key != ConsoleKey.Enter);

Console.ReadLine();
Console.WriteLine(pwd);
Console.ReadKey();





如果你想使用while循环分配key的值,就像这样:


OR
If you want to use while loop assign the value of key like this:

ConsoleKeyInfo key='\0';



祝你好运

享受!


Good Luck
Enjoy!


编写如下代码。



Write your code as below.

Console.Write("Password :"); 
ConsoleKeyInfo key;
key=Console.ReadKey(true);
while (key.Key != ConsoleKey.Enter) 
{ 
   if (key.Key != ConsoleKey.Backspace) 
   { 
     pwd += key.KeyChar; 
     Console.Write("*"); 
   } 
   key = Console.ReadKey(true); 
} 
pwd = Console.ReadLine();





这可能会对你有帮助。



This may help you.


这篇关于在*中显示密码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆