不知怎的,数组不起作用.用户输入时不存储任何值和名称

本文关键字:存储 任何值 输入 用户 数组 不起作用 | 更新日期: 2023-09-27 18:21:48

程序要求用户输入玩家的姓氏和号码。用户最多可以输入5个玩家。使用数组进行存储。

namespace While_loop_testing
{
  class Program
  {
    static void Main(string[] args)
   {
        //Maximum number of players on a team (physical array size)
          const int MAX_PLAYERS = 5;
        //Array of player numbers
        int[] playerNumbers = new int[MAX_PLAYERS];
        //Corresponding array of player last names
        string[] playerLastNames = new string[MAX_PLAYERS];
        //Actual numbers of players currently in the roster (logical array size)
        int playerCount = 0;
        char item = 'o';
        while (item == 'I' || item == 'D' || item == 'R' || item != 'X')
        {
            item = ReadMenuOption();
            if (item == 'X')
            {
                Console.WriteLine("Thanks! GoodBye!");
                Console.ReadLine();
            }
        }
    }
    /// <summary>
    /// Prompt user for the input item
    /// </summary>
    /// <returns>package</returns>
    static char ReadMenuOption()
    {
        char item;
        int MAX_PLAYERS = 25;
        int[] playerNumbers = new int[MAX_PLAYERS];
        string[] playerLastNames = new string[MAX_PLAYERS];
        int playerCount = 0;
        Console.WriteLine("Options 'n I - Insert Player 'n D - Delete Player 'n R - Player Report 'n X - Exit Program");
        do
        {
            Console.Write("Enter item here : ");
            item = Console.ReadLine().ToUpper()[0];
            if (item == 'I')
            {
                ProcessInsert(playerNumbers, playerLastNames, playerCount, MAX_PLAYERS);
                Console.WriteLine("");
            }
            else if (item == 'D')
            {
                Console.WriteLine("ProcessDeleteCalled");
                Console.WriteLine("");
            }
            else if (item == 'R')
            {
                ProcessReport(playerNumbers,playerLastNames, playerCount);
                Console.WriteLine("");
            }
            else if (item != 'I' && item != 'D' && item != 'R' && item != 'X')
            {
                Console.WriteLine("Invalid option. Please enter again");
                Console.WriteLine("");
            }
        } while (item != 'I' && item != 'D' && item != 'R' && item != 'X');
        return item;
    }
    static void ProcessInsert(int[] playerNumbers, string[] playerLastNames, int playerCount, int MAX_PLAYERS)
    {
        int numberOfPlayer, insertIndex, count;
        string prompt = "Enter player number here : ";
        string prompt1 = "Enter player Last Name here : ";
        string name;
        char response;
        if (playerCount < MAX_PLAYERS)
        {
            numberOfPlayer = GetPosNonZeroInt(prompt);
            insertIndex = GetInsertIndex(numberOfPlayer, playerNumbers, playerCount);
            if (insertIndex == -1)
            {
                Console.WriteLine(" The player number is already exists");
            }
            else
            {
                name = GetNonEmptyString(prompt1);
                for (count = playerCount; count > insertIndex; count--)
                {
                    playerNumbers[count] = playerNumbers[count - 1];
                    playerLastNames[count] = playerLastNames[count - 1];
                }
                playerCount++;
                playerLastNames[insertIndex] = name;
                numberOfPlayer = playerNumbers[insertIndex];
            }
            if (playerCount == MAX_PLAYERS)
            {
                Console.WriteLine("'nThe player roster is now full");
            }
            else
            {
                response = GetUpperYNChar("'nWould you like to insert another");
                if (response == 'N')
                {
                    Console.WriteLine("Thank you!!!");
                    Console.WriteLine("");
                }
            }
        }
        else
        {
            Console.WriteLine("'nThe player roster is already full");
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="prompt"></param>
    /// <returns></returns>
    static int GetPosNonZeroInt(string prompt)
    {
        int numberOfPlayer;
        do
        {
            Console.Write(prompt);
            numberOfPlayer = int.Parse(Console.ReadLine());
            if (numberOfPlayer <= 0)
            {
                Console.WriteLine("Invalid numbers");
                Console.WriteLine("Please enter a positive numbers");
                Console.WriteLine("");
            }
        } while (numberOfPlayer < 0);
        return numberOfPlayer;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="playerNumber"></param>
    /// <param name="playerNumbers"></param>
    /// <param name="playerCount"></param>
    /// <returns></returns>
    static int GetInsertIndex(int playerNumber, int[] playerNumbers, int playerCount)
    {
        int insertIndex = 0;
        bool found = false;
        while (insertIndex < playerCount && found == false)
        {
            if (playerNumbers[insertIndex] == playerNumber)
            {
                insertIndex = -1;
                found = true;
            }
            else if (playerNumbers[insertIndex] > playerNumber)
            {
                found = true;
            }
            else
            {
                insertIndex = insertIndex + 1;
            }
        }
        return insertIndex;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="playerNumbers"></param>
    /// <param name="playerLastNames"></param>
    /// <param name="playerCount"></param>
    static void ProcessReport(int[] playerNumbers, string[] playerLastNames, int playerCount)
    {
        Console.WriteLine("Player Number    Player Last Name");
        for (int counter = 0; counter < playerCount; counter++)
        {
            Console.WriteLine("           {0}                  {1}", playerNumbers[counter], playerLastNames[counter]);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="prompt"></param>
    /// <returns></returns>
    static string GetNonEmptyString(string prompt)
    {
        string str;
        Console.WriteLine();
        Console.Write(prompt);
        str = Console.ReadLine();
        while (str == "")
        {
            Console.WriteLine("Error - input must not be empty");
            Console.Write(prompt);
            str = Console.ReadLine();
        }
        return str;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="prompt"></param>
    /// <returns></returns>
    static char GetUpperYNChar(string prompt)
    {
        char YN;
        Console.WriteLine(prompt);
        YN = Console.ReadKey().KeyChar;
        YN = Char.ToUpper(YN);
        Console.ReadKey();
        while (YN != 'Y' && YN != 'N')
        {
            Console.WriteLine("'nInvalid Response - Please Enter Y OR N");
            YN = Console.ReadKey().KeyChar;
            YN = Char.ToUpper(YN);
            Console.ReadKey();
        }
        return Char.ToUpper(YN);
    }
  }
}

不知怎的,数组不起作用.用户输入时不存储任何值和名称

更改ProcessInsert方法中的playerCount参数时,不会影响方法外的变量。参数是按值发送的,因此参数playerCount具有方法外部playerCount变量的值的副本。

要使参数引用变量,需要ref关键字:

static void ProcessInsert(int[] playerNumbers, string[] playerLastNames, ref int playerCount, int MAX_PLAYERS)

你还需要在每个地方使用if调用的方法:

ProcessInsert(playerNumbers, playerLastNames, ref playerCount, MAX_PLAYERS);

您正在从数组中获取一个值,而不是将值放入其中

numberOfPlayer = playerNumbers[insertIndex];

应该是:

playerNumbers[insertIndex] = numberOfPlayer;