C#控制台-密码字段不起作用,需要帮助 [英] C# Console - Password Field not working, Need Help

查看:68
本文介绍了C#控制台-密码字段不起作用,需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Threading;
using System;


class Sample
{
    public static void Main()
    {
        string user;
        string password = "";
        string cor = "i";
        string pass = "1";
        do
        {
            Console.Write("Username: ");
            user = Convert.ToString(Console.ReadLine());

        } while (user != cor);
        {
            Console.Write("Enter your password: ");
            ConsoleKeyInfo key;
            do
            {
                key = Console.ReadKey(true);
                if (key.Key != ConsoleKey.Backspace)
                {
                    password += key.KeyChar;
                    Console.Write("*");
                }
                else
                {
                    if (password.Length > 0)
                    {
                        password = password.Substring(0, (password.Length - 1));
                        Console.Write("\b \b");
                    }
                }
            }
            while (key.Key != ConsoleKey.Enter);
            


            Console.WriteLine();
            Console.WriteLine("The Password You entered is : " + password);

            {
                if (password != pass)
                {
                    Console.WriteLine();
                    Console.WriteLine("PASSWORD DECLINED");
                    //Thread.Sleep(700);

                }
                else if (password == pass)
                {
                    Console.WriteLine();
                    Console.WriteLine("PASSWORD ACCEPTED");
                    //Thread.Sleep(700);
                }
                else Console.WriteLine("ERROR"); 
                Console.ReadKey();
            }
        }
    }
}


我正在使用.NET Framework 3.5
我对编程非常陌生,我不确定为什么它不起作用.
任何提示或更正将是有帮助的.


I''m using .NET framework 3.5
I''m super new to programming and I am unsure why this is not working.
Any tips or corrections would be helpful.

推荐答案

问题很简单:将每个字符添加到密码中,包括ENTER键最后.
因此,当您执行密码检查时,会将"i"与永远不会匹配的"i \ r"进行比较.

两种解决方法:
1)在密码中添加"\ r"字符.令人讨厌的暴力和无知方法.
2)重新制作循环:
The problem is quite simple: You add every character to your password, including the ENTER key at the end.
So, when you do the password check, you compare "i" with "i\r" which will never match.

Two ways round this:
1) Add the ''\r'' character to your password. Nasty, brute-force-and-ignorance approach.
2) Re-work your loop:
ConsoleKeyInfo key = Console.ReadKey(true);
while (key.Key != ConsoleKey.Enter)
    {
    if (key.Key != ConsoleKey.Backspace)
        {
        password += key.KeyChar;
        Console.Write("*");
        }
    else
        {
        if (password.Length > 0)
            {
            password = password.Substring(0, (password.Length - 1));
            Console.Write("\b \b");
            }
        }
    key = Console.ReadKey(true);
    }


您可以很容易地自己找到这个.由于问题在于两个字符串看起来不匹配,因此当您认为它们应该匹配时,您需要找出为什么不匹配.
所以.在行上放置一个断点:


You could have found this yourself, very easily. Since the problem is that the two strings don''t appear to match, when you think they should, you need to find out why not.
So. put a breakpoint on the line:

if (password != pass)

然后,程序的执行将在那里停止,您可以查看(并根据需要更改)值"password"和"pass".然后,您可以开始回顾您的代码以查找问题的根源.


但是当您在这里时:

The execution of your program will then stop there, and you can look at (and change if you need to) the values "password" and "pass". You can then start looking back in your code to find the source of the problem.


But while you are here:

if (password != pass)
    {
    ...
    }
else if (password == pass)
    {
    ...
    }
else
    ...

不需要那么复杂.由于密码等于或不等于密码,因此通常只写:

Doesn''t need to be that complex. Since password either is or isn''t equal to pass, it is normal to just write:

if (password != pass)
    {
    ...
    }
else
    {
    ...
    }

(除非您真的感到偏执,并怀疑布尔值具有三种状态:"true","false"和"quantum".在.NET中,它不是诚实的.)

(Unless you are feeling really paranoid and suspect that a bool has three states: "true", "false" and "quantum". It doesn''t, in .NET. Honest.)


//注释您的else块,并在if块中添加一个条件,然后它将正常工作
//if(key.Key!= ConsoleKey.Backspace&& key.Key!= ConsoleKey.Enter)

使用System.Threading;
使用系统;

类Sample
{
公共静态void Main()
{
字符串用户;
字符串密码=";
字符串cor ="i";
字符串传递="1";

{
Console.Write("Username:");
用户= Convert.ToString(Console.ReadLine());
} while(用户!= cor);
{
Console.Write(输入密码:");
ConsoleKeyInfo键;

{
键= Console.ReadKey(true);
if(key.Key!= ConsoleKey.Backspace&& key.Key!= ConsoleKey.Enter)
{
密码+ = key.KeyChar;
Console.Write("*");
}
/*其他
{
//if(password.Length& gt; 0)
{
//密码= password.Substring(0,(password.Length-1));
//Console.Write("\b \ b);
}
} */
}
while(key.Key!= ConsoleKey.Enter);

Console.WriteLine();
Console.WriteLine(您输入的密码为:" +密码);
{
如果(密码!=通过)
{
Console.WriteLine();
Console.WriteLine("PASSWORD DECLINED");
//Thread.Sleep(700);
}
否则,如果(password == pass)
{
Console.WriteLine();
Console.WriteLine("PASSWORD ACCEPTED");
//Thread.Sleep(700);
}
else Console.WriteLine("ERROR");
Console.ReadKey();
}
}
}
}
//comment your else block and add one more condition in if block then it will work fine
//if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter )

using System.Threading;
using System;

class Sample
{
public static void Main()
{
string user;
string password = "";
string cor = "i";
string pass = "1";
do
{
Console.Write("Username: ");
user = Convert.ToString(Console.ReadLine());
} while (user != cor);
{
Console.Write("Enter your password: ");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter )
{
password += key.KeyChar;
Console.Write("*");
}
/*else
{
//if (password.Length > 0)
{
// password = password.Substring(0, (password.Length - 1));
//Console.Write("\b \b");
}
}*/
}
while (key.Key != ConsoleKey.Enter);

Console.WriteLine();
Console.WriteLine("The Password You entered is : " + password);
{
if (password != pass)
{
Console.WriteLine();
Console.WriteLine("PASSWORD DECLINED");
//Thread.Sleep(700);
}
else if (password == pass)
{
Console.WriteLine();
Console.WriteLine("PASSWORD ACCEPTED");
//Thread.Sleep(700);
}
else Console.WriteLine("ERROR");
Console.ReadKey();
}
}
}
}


这篇关于C#控制台-密码字段不起作用,需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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