ユーザー名とパスワードの比較に配列を使用して、C# で非常に基本的なログイン システムを作成しようとしています。
ループを使用してfor()
、ユーザーが提供するユーザー名とパスワードを、配列にあるユーザー名とパスワードと比較しています。ここに私のループコードがあります:
string user = null, usrpassword = null;
string[] usernames = {"admin", "guest"};
string[] userpasswords = {"adminpw", "guestpw"};
Console.Write("Username: "); //Username
user = Console.ReadLine();
Console.Write("Password: "); //Password
usrpassword = Console.ReadLine();
Console.WriteLine("Processing...");
for (int i = 0; i <= usernames.Length; i++)
{
if (user == usernames[i] && usrpassword == userpasswords[i])
{
loginloop = false;
Console.WriteLine("Login Successful.");
}
else if (i > usernames.Length)
{
//incorrect username
Console.WriteLine("Incorrect username or password!");
}
} //for-loop-end
ビルド時に構文エラーは発生しませんが、for ループに到達するとクラッシュし、IndexOutOfRange
例外が発生します。