C# 如何在中使用数组验证用户名和密码

C# 如何在中使用数组验证用户名和密码,c#,arrays,C#,Arrays,您好,我想制作一个登录屏幕,用户将在其中输入用户名和密码。但是如何使用数组验证它?请帮忙,谢谢 int[] username = { 807301, 992032, 123144 ,123432}; string[] password = {"Miami", "LosAngeles" ,"NewYork" ,"Dallas"}; if (username[0].ToString() == password[0]) {

您好,我想制作一个登录屏幕,用户将在其中输入用户名和密码。但是如何使用数组验证它?请帮忙,谢谢

        int[] username = { 807301, 992032, 123144 ,123432};

        string[] password = {"Miami", "LosAngeles" ,"NewYork" ,"Dallas"};

        if (username[0].ToString() == password[0])
        {
            MessageBox.Show("equal");
        }
        else
        {
            MessageBox.Show("not equal");
        }

您需要首先从数组中找到用户名的索引
username
。然后根据该索引比较密码数组中的密码

int[] username = { 807301, 992032, 123144, 123432 };

string[] password = { "Miami", "LosAngeles", "NewYork", "Dallas" };

int enteredUserName = 123144;
string enteredPassword = "NewYork";

//find the index from the username array
var indexResult = username.Select((r, i) => new { Value = r, Index = i })
                          .FirstOrDefault(r => r.Value == enteredUserName);
if (indexResult == null)
{
    Console.WriteLine("Invalid user name");
    return;
}

int indexOfUserName = indexResult.Index;

//Compare the password from that index. 
if (indexOfUserName < password.Length && password[indexOfUserName] == enteredPassword)
{
    Console.WriteLine("User authenticated");
}
else
{
    Console.WriteLine("Invalid password");
}
int[]username={807301199203123144123432};
字符串[]密码={“迈阿密”、“洛杉矶”、“纽约”、“达拉斯”};
int enteredUserName=123144;
输入的字符串password=“NewYork”;
//从用户名数组中查找索引
var indexResult=username.Select((r,i)=>new{Value=r,Index=i})
.FirstOrDefault(r=>r.Value==enteredUserName);
if(indexResult==null)
{
Console.WriteLine(“无效用户名”);
返回;
}
int indexOfUserName=indexResult.Index;
//比较该索引中的密码。
if(indexOfUserName
你为什么不使用字典?字典是某种数组,但它结合了匹配的键和值
TryGetValue
将尝试查找用户名。如果未找到用户名,函数将返回
false
,否则将返回
true
和匹配的密码。此密码可用于验证用户输入的密码

Dictionary<int, string> userCredentials = new Dictionary<int, string>
{
    {807301, "Miami"},
    {992032, "LosAngeles"},
    {123144, "NewYork"},
    {123432 , "Dallas"},
};

int userName = ...;
string password = ...;

string foundPassword;
if (userCredentials.TryGetValue(userName, out foundPassword) && (foundPassword == password))
{
    Console.WriteLine("User authenticated");
}
else
{
    Console.WriteLine("Invalid password");
}
Dictionary userCredentials=新字典
{
{807301,“迈阿密”},
{992032,“洛杉矶”},
{123144,“纽约”},
{123432,“达拉斯”},
};
int用户名=。。。;
字符串密码=。。。;
字符串密码;
if(userCredentials.TryGetValue(用户名,out foundPassword)和&(foundPassword==密码))
{
Console.WriteLine(“用户认证”);
}
其他的
{
Console.WriteLine(“无效密码”);
}

如果(userName==userName[0].ToString()&&password==password[0]){}是不是应该是
,其中
userName
password
是用户输入的?是的,您是正确的,谢谢!但如何检查其他索引?