需要做一个注册,但当注册多个用户,我得到一个错误,我还需要使用类和列表c#
本文关键字:一个 注册 列表 用户 错误 | 更新日期: 2023-09-27 17:49:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace arrays
{
class Program
{
static List<Newaccount> account = new List<Newaccount>();
static void Main(string[] args)
{
int selec = 0, count = 0,selec2=0;
string quit="",sub="";
do
{
Newaccount account1 = new Newaccount("", "");
ConsoleKeyInfo letter = new ConsoleKeyInfo();
Console.Clear();
Console.WriteLine("1.Create New Account");
Console.WriteLine("2.Log In");
selec = Convert.ToInt32(Console.ReadLine());
if (selec == 1)
{
do
{
Console.Clear();
Console.WriteLine("Enter username");
account1.username = Console.ReadLine();
if (checkusername(account1.username))
{
Console.Clear();
Console.WriteLine("Username already in use");
Console.ReadLine();
Console.Clear();
}
}
while (checkusername(account1.username));
Console.WriteLine("Enter password");
account1.password = Console.ReadLine();
account.Add(account1);
}
else if (selec == 2)
{
do
{
int x = 0, y = 3;
Console.Clear();
Console.WriteLine("Enter username");
account1.username = Console.ReadLine();
Console.WriteLine("Enter Password");
do
{
letter = Console.ReadKey();
if (letter.Key != ConsoleKey.Enter)
{
account1.password = account1.password + letter.KeyChar;
Console.SetCursorPosition(x, y);
Console.Write("*");
x++;
}
}
while (letter.Key != ConsoleKey.Enter);
if (checklogin(account1.username, account1.password))
{
do
{
Console.Clear();
Console.WriteLine("Username and password correct");
Console.ReadLine();
Console.WriteLine("What do you wish to do");
Console.WriteLine("1.Delete account");
Console.WriteLine("2.Return to Menu");
selec2 = Convert.ToInt32(Console.ReadLine());
if (selec2 == 1)
{
account.Remove(account1);
}
else if (selec2 >= 3 || selec2 <= 0)
{
Console.Clear();
Console.WriteLine("No such selecetion is available");
Console.ReadLine();
}
}
while (selec2 != 1 && selec2 != 2);
}
else
{
Console.Clear();
Console.WriteLine("Incorrect Username or password try again");
Console.ReadLine();
}
count++;
}
while (!checklogin(account1.username, account1.password) && (count < 5));
}
if (count < 5)
{
Console.Clear();
Console.WriteLine("Do you wish to exit Y/N");
quit = Console.ReadLine();
}
else
{
Console.Clear();
Console.WriteLine("Too many incorrect tries");
Console.ReadLine();
}
}
while (quit != "y" && quit != "Y" && count < 5);
}
static bool checkusername(string username)
{
int i = 0;
bool found= false;
if (account.Count == 0)
{
return found;
}
else
{
do
{
if (account[i].username == username)
{
found = true;
}
i++;
}
while ((i < account.Count) || (!(found)));
return found;
}
}
static bool checklogin(string user, string pword)
{
int i = 0;
bool found = false;
if (account.Count == 0)
{
return found;
}
else
{
do
{
if (account[i].password == pword && account[i].username == user)
{
found = true;
}
i++;
}
while ((i < account.Count) && (!(found)));
}
return found;
}
}
}
class Newaccount
{
public string username = "", password ="";
public Newaccount(string username,string password)
{
this.username = username;
this.password= password;
}
我认为你的问题是在你的checkusername方法:
do
{
if (account[i].username == username)
{
found = true;
}
i++;
}
while ((i < account.Count) || (!(found)));
特别是
这行while ((i <)account.Count) | |(!(发现)));
如果用户名不存在,那么这将导致do/while循环继续遍历列表,即使已经达到上范围,导致抛出indexoutorange异常。
您可以将其更改为:
while ((i <)account.Count),,(!(发现)));
,但更简单的检查方法是使用LINQ提供的Any()方法:
static bool checkusername(string username)
{
return account.Any(u => u.username == username);
}