简体   繁体   English

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

[英]How to add c# login attempts loop in console application?

I'm new to this language, I tried things but couldn't figure out how to set a login loop to use login attemps with a max.我是这种语言的新手,我尝试过一些事情,但无法弄清楚如何设置登录循环以使用最大登录次数。 of 3 times. 3 次。 Could someone help me out?有人可以帮我吗?

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

} }

You should add the for loop for maximum number of attempts.您应该添加 for 循环以获得最大尝试次数。 For reference you can read below作为参考,您可以阅读以下内容

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

You can use for loop accordingly whether you want the user to enter password only or username and password both您可以相应地使用 for 循环,无论您是希望用户只输入密码还是同时输入用户名和密码

Why not recursive为什么不递归

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


        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM