C# 为什么';即使密码不';不匹配?
我的程序应该无限期地要求用户重新输入用户名和密码,直到它匹配他们的第一次输入,但是循环只运行一次。我已经切断了当用户在三次尝试中输入正确的详细信息时工作的部分,所以请不要要求我重写它,否则,此提交将无法到达更有经验的开发人员。请帮我解决这个问题,因为我真的想让这个程序运行C# 为什么';即使密码不';不匹配?,c#,loops,do-while,C#,Loops,Do While,我的程序应该无限期地要求用户重新输入用户名和密码,直到它匹配他们的第一次输入,但是循环只运行一次。我已经切断了当用户在三次尝试中输入正确的详细信息时工作的部分,所以请不要要求我重写它,否则,此提交将无法到达更有经验的开发人员。请帮我解决这个问题,因为我真的想让这个程序运行 Console.WriteLine(“拒绝输入”); Console.WriteLine(“请重新输入您的用户名:”); 字符串username3=Console.ReadLine(); Console.WriteLine(“
Console.WriteLine(“拒绝输入”);
Console.WriteLine(“请重新输入您的用户名:”);
字符串username3=Console.ReadLine();
Console.WriteLine(“请重新输入您的密码”);
字符串password3=Console.ReadLine();
做
{
Console.WriteLine(“请再次输入您的用户名:”);
字符串username4=Console.ReadLine();
Console.WriteLine(“请再次输入您的密码”);
字符串password4=Console.ReadLine();
如果(用户名==用户名4)
{
usernamematch=true;
if(usernamematch==true)
{
如果(密码==密码4)
{
passwordmatch=true;
if(passwordmatch==true)
{
WriteLine(“此时,您将被重定向到我们的网页,但这是c#programming。”);
打破
}
其他的
{
}
}
}
}
其他的
{
}
}while(username!=username3 | | password!=password3);
这仍然有您的无限循环(不推荐),但它可以完成任务。在while循环bool中,除了“true”之外,不需要使用任何东西,因为您将使用“break;”来打破它
什么是用户名?那套怎么样?它是否应该检查username3=username4?。你能发布完整的代码吗?你正在退出while循环。取出
返回框代码>语句。您的所有代码路径都以返回
或中断
结束,这两者都退出循环。设置passwordmatch=true代码>在一行,然后在下一行检查是否(passwordmatch==true)
似乎有点不必要……我想你是个学生吧。请注意,您有一个潜在的无限循环,因为不能保证while
条件将永远停止。更好的设计是限制尝试次数,并使用for
循环。
Console.WriteLine("Entry denied");
Console.WriteLine("Please re-enter your username:");
string username3 = Console.ReadLine();
Console.WriteLine("Please re-enter your password");
string password3 = Console.ReadLine();
while (true)
{
Console.WriteLine("Please re-enter your username once more:");
string username4 = Console.ReadLine();
Console.WriteLine("Please re-enter your password once more");
string password4 = Console.ReadLine();
usernamematch = username == username4;
passwordmatch = password == password4;
if (usernamematch && passwordmatch)
{
Console.WriteLine("You, at this point, would be redirected to our webpage but this is c# programming.");
break;
}
}