我需要创建一个简单的 C# Sharp 程序,它将用户 ID 和密码作为输入(字符串类型)。 3 次错误尝试后,用户应被拒绝。
我已经开始,但我不确定应该如何正确完成逻辑。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UserId
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type username");
String UserId1 = Console.ReadLine();
Console.WriteLine("Type password");
String Pass = Console.ReadLine();
String UserIdCorrect = "test1";
String PassCorrect = "password1";
int MaxAttempts = 3;
Console.ReadKey();
if (UserId1 != UserIdCorrect && Pass != PassCorrect ) {
MaxAttempts++;
}
Console.ReadKey();
}
}
}
最佳答案
首先,即使在编写一行代码之前,也请尝试花一两分钟考虑命名约定。这就像“剃须前先在脸上抹上泡沫,即使没有剃须泡沫也能剃须,但体验不会很好”。尝试此链接以获取更多信息 [https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx] 。
现在转向你的问题,如果我只需满足你的要求,这段代码就足够了。在这里,我将“valid”作为正确凭据的标识符:
<code>
//Login Attempts counter
int loginAttempts = 0;
//Simple iteration upto three times
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Enter username");
string username = Console.ReadLine();
Console.WriteLine("Enter password");
string password = Console.ReadLine();
if (username != "valid" || password != "valid")
loginAttempts++;
else
break;
}
//Display the result
if (loginAttempts > 2)
Console.WriteLine("Login failure");
else
Console.WriteLine("Login successful");
Console.ReadKey();
</code>
关于c# - 简单的 C# 登录,尝试 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861283/