在c#数组中查找数据的问题

本文关键字:数据 问题 查找 数组 | 更新日期: 2023-09-27 18:12:15

我需要编写一个计算一顿饭价格的程序,这个程序需要保存100条饭的记录,并且可以编辑、查找、删除。问题是,即使我输入信息,我也找不到它或显示成本。这是我代码的一部分,因为我不能输入所有行代码,这是我的完整代码链接:http://ideone.com/1EGzyB

   //Add an record to array:
        static void AddFunction(string[] ClientNameArray, string[] AddressArray, string[] PhoneNumberArray, int[] NumberOfGuestsArray, string[] MealTypeArray, int[] YearArray, int[] MonthArray, int[] DayArray, ref int NextAvailablePosition)
        {
            string ClientName = "";
            string Address = "";
            string PhoneNumber = "";
            int NumberOfGuests = 0;
            string MealType = "";
            int Year = 0;
            int Month = 0;
            int Day = 0;
            bool ParseResult = false;
            bool ErrorFlag = false;
            string UserResponse = ""; // Used to hold temp values entered from the keyboard that will be converted or modified before storing a value
            int MaxDays = 0; // Used to store the maximum number of days in any given month
            string MonthName = "";
            do
            {
                ErrorFlag = false;
                Console.Write("Name of client              : ");
                ClientName = Console.ReadLine();
                if (ClientName == "")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Client Name must not be left blank.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            do
            {
                ErrorFlag = false;
                Console.Write("Address of function         : ");
                Address = Console.ReadLine();
                if (Address == "")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Address must not be left blank.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            do
            {
                ErrorFlag = false;
                Console.Write("Contact telephone number    : ");
                PhoneNumber = Console.ReadLine();
                if (PhoneNumber == "")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Phone Number must not be left blank.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            do
            {
                ErrorFlag = false;
                Console.Write("Number of guests            : ");
                UserResponse = Console.ReadLine();
                ParseResult = int.TryParse(UserResponse, out NumberOfGuests);
                if (ParseResult == false)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Not a valid number.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
                else if (NumberOfGuests <= 0 || NumberOfGuests > 100)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Number of Guests must be a number greater than 0 and not more than 100.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            do
            {
                ErrorFlag = false;
                Console.WriteLine("Meal type - Enter ");
                Console.WriteLine("    F for Finger food,");
                Console.WriteLine("    2 for 2 course,");
                Console.WriteLine("    3 for 3 course or");
                Console.Write("    B for Banquet)          : ");
                MealType = Console.ReadLine();
                MealType = MealType.ToUpper();
                if (MealType != "F" && MealType != "2" && MealType != "3" && MealType != "B")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Meal type must be one of F, 2, 3, or B. Press ENTER to exit");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            // Get the date as a year, month and day in that order as integers
            // Validate the year by checking that it is 2015 or later
            do
            {
                ErrorFlag = false;
                Console.WriteLine("Date of function");
                Console.Write("    Year                    : ");
                UserResponse = Console.ReadLine();
                ParseResult = int.TryParse(UserResponse, out Year);
                if (ParseResult == false)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Not a valid number.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
                else if (Year < 2015)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("That year has already passed. Please enter a year from 2015 onwards'r'n... or invent a time machine to go back to the year you entered :)");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);
            // Validate the month by checking that it is between 1 and 12 inclusive
            do
            {
                ErrorFlag = false;
                Console.Write("    Month                   : ");
                UserResponse = Console.ReadLine();
                ParseResult = int.TryParse(UserResponse, out Month);
                if (ParseResult == false)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Not a valid number. Press ENTER to exit");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
                else if (Month < 1 || Month > 12)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The month must be between 1 and 12 inclusive. Press ENTER to exit");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);

            // Work out how many days there are in the selected month
            switch (Month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: MaxDays = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11: MaxDays = 30;
                    break;
                case 2:
                    // Check for the number of days in February
                    // If the year is evenly divisible by 4 it is a leap year
                    if (Year % 4 == 0)
                        MaxDays = 29;
                    else
                        MaxDays = 28;
                    break;
                default:
                    // This should never occur
                    Console.WriteLine("An unexpected error has occurred. Press ENTER to exit");
                    Console.ReadLine();
                    Environment.Exit(0);
                    break;
            }
            // Validate the Day by checking that it is a positive integer and that it does not exceed the length of the month
            do
            {
                ErrorFlag = false;
                Console.Write("    Day                     : ");
                UserResponse = Console.ReadLine();
                ParseResult = int.TryParse(UserResponse, out Day);
                if (ParseResult == false)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Not a valid number.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
                else if (Day <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Day must be 1 or greater.");
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
                else if (Day > MaxDays)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid date. {0} has {1} days.", MonthName, MaxDays);
                    Console.ForegroundColor = ConsoleColor.White;
                    ErrorFlag = true;
                }
            } while (ErrorFlag);
            ClientName = ClientNameArray[NextAvailablePosition];
            Address = AddressArray[NextAvailablePosition];
            PhoneNumber = PhoneNumberArray[NextAvailablePosition];
            NumberOfGuests = NumberOfGuestsArray[NextAvailablePosition];
            MealType = MealTypeArray[NextAvailablePosition];
            Year = YearArray[NextAvailablePosition];
            Month = MonthArray[NextAvailablePosition];
            Day = DayArray[NextAvailablePosition];
        }
        static void FindFunction(string[] ClientNameArray, string[] AddressArray, string[] PhoneNumberArray, int[] NumberOfGuestsArray, string[] MealTypeArray, int[] YearArray, int[] MonthArray, int[] DayArray, int ARRAY_SIZE, ref int CurrentRecord)
        {
            string ClientName = "";
            Console.Write("Enter name of customers that you want to find: ");
            ClientName = Console.ReadLine();
                if (ClientNameArray[CurrentRecord] == ClientName)
                {
                    Console.WriteLine("Name of Customers: ", ClientNameArray[CurrentRecord]);
                    Console.WriteLine("Address          : ", AddressArray[CurrentRecord]);
                    Console.WriteLine("Phone Number     : ", PhoneNumberArray[CurrentRecord]);
                    Console.WriteLine("Meal Type        : ", MealTypeArray[CurrentRecord]);
                    Console.WriteLine("Number of Guests: ", NumberOfGuestsArray[CurrentRecord]);
                    Console.WriteLine("Date: {0} / {1} / {2}", DayArray[CurrentRecord], MonthArray[CurrentRecord], YearArray[CurrentRecord]);
                }
                else
                {
                    Console.Write("Not found! Try another name.");
            }

        }

在c#数组中查找数据的问题

没有循环遍历数组。您所比较的只是客户端名称与当前记录

if (ClientNameArray[CurrentRecord] == ClientName)

应该

while(not the end of the array)
{
   if (ClientNameArray[CurrentRecord] == ClientName)
   {
       Do stuff
   }
}

你应该有一个数组来存储一个类型为RestaurantVisit(或类似的东西)的对象,RestaurantVisit将有客户端名称,地址等属性