如何在控制台应用程序中添加c#登录尝试循环?

我是这种语言的新手,我尝试过但无法弄清楚如何设置登录循环以使用最大值的登录尝试。 3次。 有人可以帮帮我吗?

static void Main(string[] args) { Console.WriteLine("Status: " + status.Onaangemeld); Console.WriteLine("Welkom, typ hieronder het gebruikersnaam:"); string Naam = Console.ReadLine(); Console.WriteLine("Vul hieronder het wachtwoord in:"); string Wachtwoord = Console.ReadLine(); if (Naam == "gebruiker" && Wachtwoord == "SHARPSOUND") { Console.Clear(); Console.WriteLine("Status: " + status.Ingelogd); Console.WriteLine("Welkom bij SoundSharp {0}!", Naam); Console.ReadLine(); } else Console.Clear(); Console.WriteLine("Helaas, gebruikersnaam of wachtwoord niet correct."); } } 

 static void Main(string[] args) { for (int i=0; i<3; i++) { Console.WriteLine("Status: " + status.Onaangemeld); Console.WriteLine("Welkom, typ hieronder het gebruikersnaam:"); string Naam = Console.ReadLine(); Console.WriteLine("Vul hieronder het wachtwoord in:"); string Wachtwoord = Console.ReadLine(); if (Naam == "gebruiker" && Wachtwoord == "SHARPSOUND") { Console.Clear(); Console.WriteLine("Status: " + status.Ingelogd); Console.WriteLine("Welkom bij SoundSharp {0}!", Naam); Console.ReadLine(); break; } Console.Clear(); Console.WriteLine("Helaas, gebruikersnaam of wachtwoord niet correct."); } Console.Clear(); Console.WriteLine("...."); } 

}

您应该添加for循环以获得最大尝试次数。 供参考,您可以在下面阅读

https://msdn.microsoft.com/en-us/library/ch45axte.aspx

无论您希望用户只输入密码还是用户名和密码,都可以相应地使用for循环

为什么不递归

  class Program { const int MaxAttempt = 3; static int currentAttempt = 0; static void Main(string[] args) { if (MaxAttempt == currentAttempt) { Console.WriteLine("You have reached maximum try .. please come after some time"); Console.ReadLine(); Environment.Exit(0); } currentAttempt++; Console.WriteLine("Status: " + status.Onaangemeld); Console.WriteLine("Welkom, typ hieronder het gebruikersnaam:"); string Naam = Console.ReadLine(); Console.WriteLine("Vul hieronder het wachtwoord in:"); string Wachtwoord = Console.ReadLine(); if (Naam != "gebruiker" || Wachtwoord != "SHARPSOUND") { Console.Clear(); Console.WriteLine("Helaas, gebruikersnaam of wachtwoord niet correct. Please try again"); Console.ReadLine(); Console.Clear(); Main(args); } Console.Clear(); Console.WriteLine("Status: " + status.Ingelogd); Console.WriteLine("Welkom bij SoundSharp {0}!", Naam); Console.ReadLine(); } }