c# - 简单的 C# 登录,尝试 3 次

标签 c# console login-attempts

我需要创建一个简单的 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/

相关文章:

c# - Request.PathInfo 问题和 XSS 攻击

java - 通过java控制台逐行输入

linux - kcachegrind 的控制台替代品?

java - 将控制台输出重定向到Java中的字符串

c# - 如何验证 ASP.NET MVC 中的文本框

c# - MySQL 和 LINQ to SQL 通过 Connector/Net | C#

c# - 带有 NTLM 身份验证的 HttpWebRequest 401

php - 为什么 OpenInviter 在我的本地计算机上获得经过身份验证的登录,但在网络服务器上却没有?

c# - MessageBox.Show 的简单 "Login Attempt"问题