Csharp/C#教程:如何在控制台应用程序中添加c#登录尝试循环?分享


如何在控制台应用程序中添加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循环

为什么不递归

上述就是C#学习教程:如何在控制台应用程序中添加c#登录尝试循环?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  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(); } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/950446.html

(0)
上一篇 2021年11月17日
下一篇 2021年11月17日

精彩推荐