【发布时间】:2021-11-09 10:07:00
【问题描述】:
我正在尝试在 C# 中创建密码检查器,当我为用户名和密码输入较小的值时,程序按预期执行,但是当我输入较大的值时,程序不执行任何操作。 if 语句不验证更大的输入。这是我的代码:
Console.Write("Set a username: ");
string username = Console.ReadLine();
Console.Write("Set a password: ");
string password = Console.ReadLine();
Console.WriteLine("Are you sure that you want these as your username and password? (Y/N)");
Console.Write("Enter here: ");
string decision = Console.ReadLine();
if (decision == "Y" || decision == "y")
{
Console.Write("Enter your username: ");
string inputUser = Console.ReadLine();
Console.Write("Enter your password: ");
string inputPass = Console.ReadLine();
if (inputUser == username && inputPass == password)
{
Console.WriteLine("Username and password match correctly! Restart the program to try again.");
}
else if (inputUser == username && inputPass != password || inputUser != username && inputPass == password)
{
int i = 0;
while (inputUser == username && inputPass != password || inputUser != username && inputPass == password && i < 2)
{
Console.WriteLine("Username or password don't match correctly. Please try again.");
Console.Write("Enter your username: ");
inputUser = Console.ReadLine();
Console.Write("Enter your password: ");
inputPass = Console.ReadLine();
i++;
if (inputUser == username && inputPass == password)
{
Console.WriteLine("Username and password match correctly! Restart the program to try again.");
break;
}
if (i >= 2)
{
Console.WriteLine("Too many attempts. Please restart program to try again.");
break;
}
}
}
没有来自 Visual Studio 的错误消息,所以我不知道该怎么做。 编辑: 感谢所有回答的人。我考虑了您的所有建议,现在该程序按预期运行。如果您想知道,这里是完成的代码:
Console.Write("Set a username: ");
string username = Console.ReadLine();
Console.Write("Set a password: ");
string password = Console.ReadLine();
Console.WriteLine("Are you sure that you want these as your username and password? (Y/N)");
Console.Write("Enter here: ");
string decision = Console.ReadLine();
if (decision == "Y" || decision == "y")
{
Console.Write("Enter your username: ");
string inputUser = Console.ReadLine();
Console.Write("Enter your password: ");
string inputPass = Console.ReadLine();
if (inputUser == username && inputPass == password)
{
Console.WriteLine("Username and password match correctly! Restart the program to try again.");
}
else
{
int i = 0;
while ((inputUser == username && inputPass != password) || (inputUser != username && inputPass == password) || (inputUser != username && inputPass != password) && i < 2)
{
Console.WriteLine("Username and/or password don't match correctly. Please try again.");
Console.Write("Enter your username: ");
inputUser = Console.ReadLine();
Console.Write("Enter your password: ");
inputPass = Console.ReadLine();
i++;
if (inputUser == username && inputPass == password)
{
Console.WriteLine("Username and password match correctly! Restart the program to try again.");
break;
}
if (i >= 2)
{
Console.WriteLine("Too many attempts. Please restart program to try again.");
break;
}
}
}
}
【问题讨论】:
-
您是否使用断点单步执行您的代码?如果您还没有,或者您不知道我在说什么,请放弃您正在做的事情并去了解断点。当您需要对代码行为进行故障排除时,这是最基本的工具之一。
-
你的 else if 是多余的,可以缩写为
else。我要说的一件事;当混合 AND 和 OR 时,总是(总是)使用括号向 C# 和其他阅读你作品的编码人员清楚地表明你的意思。(name = "x" and age = 1) or job = "coder"与name = "x" and (age = 1 or job = "coder")非常不同 -
在你的“else if”中,你似乎在测试“用户名和密码中的一个是正确的”,所以你错过了“密码和用户名都不正确”。正如 Caius Jard 所说,只需使用“else”(删除“if(...)”)
-
仅供参考,当您找到解决方案时,您可以回答您自己的问题。请(也)然后描述更改,而不仅仅是新代码的转储
-
提示:在
while中您可以只检查(inputUser != username || inputPass != password) && i < 2(“如果用户名和/或密码错误...”)
标签: c# string if-statement