C# 需要进行注册,但当注册多个用户时,我会遇到一个错误,我还需要使用类和列表c

C# 需要进行注册,但当注册多个用户时,我会遇到一个错误,我还需要使用类和列表c,c#,class,list,C#,Class,List,我认为您遇到的问题在于checkusername方法: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace arrays { class Program { static List<Newaccount> account = new List<Newaccount>(); stati

我认为您遇到的问题在于checkusername方法:

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;
    }

欢迎来到StackOverflow。为了帮助我们帮助您,请不要张贴代码墙。尝试创建最小的代码片段来重现错误。这样,我们可以快速诊断问题。您可能会发现,在解构和缩减您自己的代码时,您自己也会发现问题。什么错误?哪里你是怎么得到它的?在一个页面上大量的代码并不会让它成为一个问题。请问一个适当的问题。读这篇文章:事实上,要纠正这个方法-当我do { if (account[i].username == username) { found = true; } i++; } while ((i < account.Count) || (!(found)));
static bool checkusername(string username)
{
    return account.Any(u => u.username == username);
}