0

所以我之前问过这个问题,但似乎无处可去,所以我想我会再问一次,也许有人可以帮忙,因为我已经让我的代码的另一部分工作了。所以我需要知道如何更新和删除条目。我想澄清一下,我只想通过用户输入来做到这一点。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayerSystem6
{
    class Program
    {
        static void Main(string[] args)
        {
            //The MAXPLAYERS constant is the physical table size
            const Int32 MAXPLAYERS = 23;

            //Declare the player tables
            Int32[] playerNumbers = new Int32[MAXPLAYERS];
            String[] playerLastNames = new String[MAXPLAYERS];
            Int32[] playerPoints = new Int32[MAXPLAYERS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 playerCount = 0;

            //Main Driver
            char menuItem;
            Console.WriteLine("Welcome to the player system...\n");
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {
                ProcessMenuItem(menuItem, playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                menuItem = GetMenuItem();
            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }

        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            while (menuItem != 'C' && menuItem != 'R' //JG
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'U')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
            Console.WriteLine("\nPlease pick an item:");
            Console.WriteLine("C - Create Player");
            Console.WriteLine("R - Retrieve Player");
            Console.WriteLine("U - Update Player");
            Console.WriteLine("D - Delete Player");
            Console.WriteLine("L - List Players");
            Console.WriteLine("X - Exit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            switch (menuItem)
            {
                case 'C':
                    ProcessCreate(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                    break;
                case 'L':
                    ProcessList(playerNumbers, playerLastNames, playerPoints, playerCount);
                    break;
                case 'R': //JG
                    ProcessRetrieve(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                    break;
                case 'U':
                    ProcessUpdate(playerNumbers, playerLastNames, playerPoints, playerCount, MAXPLAYERS);
                    break;

            }
        }

        //Creates a player in the tables if the array is not already full and the name is not a duplicate
        static void ProcessCreate(Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            Int32 number, points;
            String lastName;
            if (playerCount < MAXPLAYERS)
            {
                number = GetPositiveInteger("\nCreate Player: please enter the player's number");
                if (GetPlayerIndex(number, playerNumbers, playerCount) == -1)
                {
                    lastName = GetString("\nCreate Player: please enter the player's last name");
                    points = GetPositiveInteger("\nCreate Player: please enter the player's points");
                    InsertPlayer(number, lastName, points, playerNumbers, playerLastNames, playerPoints, ref playerCount);
                    Console.WriteLine("\nCreate Player: Number - {0}, Name - {1}, Points - {2}, created successfully", number, lastName, points);
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nCreate Player: the player number already exists");
            }
            else
                Console.WriteLine("\nCreate Player: the player roster is already full");

        }

        //Inserts the player at the correct location in the tables based on order of 
        //ascending player number. Unless the insert location is at the end, this 
        //requires shifting existing players down in order to make room 
        static void InsertPlayer(Int32 number, String lastName, Int32 points,
            Int32[] playerNumbers, String[] playerLastNames, Int32[] playerPoints,
            ref Int32 playerCount)
        {
            /* PUTS DATA IN NEXT AVALIABLE SLOT
            playerNumbers[playerCount] = number;
            playerLastNames[playerCount] = lastName;
            playerPoints[playerCount] = points;
            playerCount++;
            */

            // PUTS DATA IN PLAYER ASECENDING ORDER
            Int32 insertIndex, shiftCount;
            insertIndex = GetInsertIndex(number, playerNumbers, playerCount);
            for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
            {
                playerNumbers[shiftCount] = playerNumbers[shiftCount - 1];
                playerLastNames[shiftCount] = playerLastNames[shiftCount - 1];
                playerPoints[shiftCount] = playerPoints[shiftCount - 1];
            }
            playerNumbers[insertIndex] = number;
            playerLastNames[insertIndex] = lastName;
            playerPoints[insertIndex] = points;
            playerCount++;

        }
        //Returns the index of the first player number in the table that is greater
        //than the player number to be inserted
        static Int32 GetInsertIndex(Int32 playerNumber, Int32[] playerNumbers,
            Int32 playerCount)
        {
            Int32 index = 0;
            bool found = false;
            while (index < playerCount && found == false)
                if (playerNumbers[index] > playerNumber)
                    found = true;
                else
                    index++;
            return index;
        }

        //Returns the index of the player number in the table 
        //or -1 if the number is not found
        static Int32 GetPlayerIndex(Int32 playerNumber,
            Int32[] playerNumbers, Int32 playerCount)
        {
            Int32 index = 0;
            bool found = false;
            while (index < playerCount && found == false)
                if (playerNumbers[index] == playerNumber)
                    found = true;
                else
                    index++;
            if (found == false)
                index = -1;
            return index;
        }

        //Lists the players in the tables
        static void ProcessList(Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, Int32 playerCount)
        {

            if (playerCount > 0)
            {
                Console.WriteLine("\n{0,7}   {1,-25}{2,6}\n", "Number", "Last Name", "Points");
                for (Int32 player = 0; player < playerCount; player++)
                    Console.WriteLine("{0,7}   {1,-25}{2,6}", playerNumbers[player], playerLastNames[player], playerPoints[player]);
            }
            else
                Console.WriteLine("\nList Players: the roster is empty");
        }

        //Returns a positive integer
        static Int32 GetPositiveInteger(String prompt)
        {
            Int32 n;
            Console.WriteLine(prompt);
            n = Int32.Parse(Console.ReadLine());
            while (n < 0)
            {
                Console.WriteLine("\nError: enter positive value");
                Console.WriteLine(prompt);
                n = Int32.Parse(Console.ReadLine());
            }
            return n;
        }

        //Returns a non-empty string
        static String GetString(String prompt)
        {
            String returnString;
            Console.WriteLine(prompt);
            returnString = Console.ReadLine();
            while (returnString == "")
            {
                Console.WriteLine("\nError: must enter keyboard data");
                Console.WriteLine(prompt);
                returnString = Console.ReadLine();
            }
            return returnString;
        }
        // retrieve single value from an array

        //static void ProcessRetrieve(Int32[] playerNumbers, String[] playerLastNames
        //, Int32[] playerPoints, Int32 playerCount)
        //{

        //}

所以在这里我开始检索单个条目的过程,该条目有效并且是一个我可以通过修改以使用接下来的两个函数来重用的函数。

        static void ProcessRetrieve(Int32[] playerNumbers, String[] playerLastNames,
            Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            int player;// Player number to find
            int playerindex;//index of the player number in Array
            if (playerCount < MAXPLAYERS)
            {
                player = GetPositiveInteger("\nRetrieve Player: please enter the player's number"); //JG I used the same mechanism when you are creating the record, when you create yu check if it exists
                /** If it exists you say record exists in this case I said display the existing record**/
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
                if (playerindex != -1)// !-1 means Does not exist JG
                {

                    Console.WriteLine("{0,7}   {1,-25}{2,6}", playerNumbers[playerindex], playerLastNames[playerindex], playerPoints[playerindex]);
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nRetrieve Player: the player number does not exists");
            }
            else
                Console.WriteLine("\nRetrieve Player: the player does not exist in the roster");
        }

现在这是我遇到困难的地方,如何更新此代码而不是检索我在这里完全迷失了,我查看了很多文章,只发现如何通过代码而不是用户输入来更改数组

        static void ProcessUpdate(Int32[] playerNumbers,
        string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS)
        {
            int player;// Player number to find
            int playerindex;//index of the player number in Array
            if (playerCounts < MAXPLAYERS || playerCounts == MAXPLAYERS)
            {
                player = GetPositiveInteger("\nUpdate Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCounts);
                if (playerindex != -1)
                {

                    Console.WriteLine("{0,7}   {1,-25}{2,6}", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]);
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nUpdate Player: the player number does not exists");
            }
            else
                Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
        }

    }
}
4

4 回答 4

1

在您的代码中,您已经拥有创建更新函数所需的构建块。让我们来看看这些是什么。

  1. 您必须先询问玩家编号。您已经在 ProcessUpdate 方法中实现了这一点。

            player = GetPositiveInteger("\nUpdate Player: please enter the player's number");
    
  2. 您必须找到与玩家编号匹配的数组索引。您也已经在 ProcessUpdate 方法中实现了这一点。

            playerindex = GetPlayerIndex(player, playerNumbers, playerCounts);
    
  3. 您需要向用户询问新的玩家数据。您已经在 ProcessCreate 方法中实现了这一点 - 只需更改字符串以阐明我们现在要求更新数据。

                lastName = GetString("\nUpdate Player: please enter the player's updated last name");
                points = GetPositiveInteger("\nUpdate Player: please enter the player's updated points");
    
  4. 最后,您必须将刚刚从用户那里获得的数据放入数组中。您已经在 InsertPlayer 中实现了这一点 - 我们只注意在这里使用正确的变量名。

        playerLastNames[playerindex] = lastName;
        playerPoints[playerindex] = points;
    


将所有这些放在一起(包括变量声明和完整性检查),您的 ProcessUpdate 方法应如下所示:

    static void ProcessUpdate(Int32[] playerNumbers,
    string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS)
    {
        int player;// Player number to find
        int playerindex;//index of the player number in Array

        String lastName;
        int points;

        if (playerCounts < MAXPLAYERS || playerCounts == MAXPLAYERS)
        {
            player = GetPositiveInteger("\nUpdate Player: please enter the player's number");
            playerindex = GetPlayerIndex(player, playerNumbers, playerCounts);
            if (playerindex != -1)
            {
                lastName = GetString("\nUpdate Player: please enter the player's updated last name");
                points = GetPositiveInteger("\nUpdate Player: please enter the player's updated points");

                playerLastNames[playerindex] = lastName;
                playerPoints[playerindex] = points;

            }
            else
                Console.WriteLine("\nUpdate Player: the player number does not exists");
        }
        else
            Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
        }
    }

一般来说,您的程序结构并不是如何正确执行它的示例(对不起)。但是,我不知道您的编程课程。您的课程是否已经涵盖了结构、对象(面向对象编程)或其他“高级”数据类型(如字典),或者这些会是未来的主题吗?我知道您需要使用 3 个数组,但是已经在 struct 的帮助下,您可以在满足要求的同时显着清理您的代码。

于 2013-10-19T03:08:23.117 回答
0

下面我写了一个示例,说明如何通过用户输入更新玩家的分数和姓名。通过将数字、名称和点的索引保持在每个玩家的相同索引中,我们可以跟踪它们。

// Make sure the indexes of the arrays are aligned with the player array 
// (to keep track which points are for which player)
int[] playerNums = new int[10];
string[] playerNames = new string[10];
int[] playerPoints = new int[10];

// We now add the user here (you do it somewhere from user input)
playerNums[0] = 1;
playerNames[0] = "Tim";
playerPoints[0] = 10;

char key = Console.ReadKey(true).KeyChar;
if (char.IsDigit(key))
{
    // We get the number from the char.
    int inputNum = int.Parse(key.ToString());

    // We make sure the user isn't giving an index past the length of our arrays
    // (which are 10 in size).
    if (inputNum > -1 && inputNum < playerNums.Length - 1)
    {
        playerNames[inputNum] = "John"; // Tim now becomes John.
        playerPoints[inputNum] += 5; // Increase John's score.
    }
}

告诉我这是否是您正在寻找的答案。

于 2013-10-19T02:38:17.857 回答
0

代替数组使用字典,如下所示,因此您无需为所有数组维护任何索引,如果您想搜索特定元素,这非常容易。球员号码是每个球员信息的关键,你可以选择任何你想要的关键。有关详细信息,请参阅以下链接 http://www.dotnetperls.com/dictionary

命名空间播放器 { 类程序 {

    static void Main(string[] args)
    {
        Dictionary<string, PlayerInfo> info = new Dictionary<string, PlayerInfo>();
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Enter Player Number");
            string playerNumber = Console.ReadLine();
            info.Add(playerNumber, new PlayerInfo());
            Console.WriteLine("Enter Player Name");
            info[playerNumber].PlayerName = Console.ReadLine(); 
            Console.WriteLine("Enter Player Points");
            info[playerNumber].points = Console.ReadLine(); 
        }

        Console.WriteLine("Enter the player number to be deleted");
        string playerNumberToDelete = Console.ReadLine();
        info.Remove(playerNumberToDelete);

        Console.WriteLine("Enter a player number to update");
        string playerNumberToUpdate = Console.ReadLine();
        Console.WriteLine("Enter Player Name");
        info[playerNumberToUpdate].PlayerName = Console.ReadLine();
        Console.WriteLine("Enter Player Points");
        info[playerNumberToUpdate].points = Console.ReadLine();

        Console.WriteLine("Enter player number dispaly deatils");
        string playerNumberToDisplay = Console.ReadLine();
        Console.WriteLine("Name " + info[playerNumberToDisplay].PlayerName);
        Console.WriteLine("Points " + info[playerNumberToDisplay].points);
    }
}
class PlayerInfo
{
    public string PlayerName;
    public string points;
}

}

于 2013-10-19T03:08:28.353 回答
0

您的 ProcessUpdate 应如下所示

     static void ProcessUpdate(Int32[] playerNumbers, string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS)
        {
            int player;// Player number to find
            int playerindex;//index of the player number in Array
            if (playerCounts <= MAXPLAYERS)
            {
                player = GetPositiveInteger("\nUpdate Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCounts);
                if (playerindex != -1)
                {
                    string lastName = GetString("\nCreate Player: please enter the player's last name");
                    int points = GetPositiveInteger("\nCreate Player: please enter the player's points");
                    playerLastnames[playerindex] = lastName;
                    playerpoints[playerindex] = points;
                    Console.WriteLine("{0,7}   {1,-25}{2,6}", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]);
                    Console.WriteLine();
                }
                else
                    Console.WriteLine("\nUpdate Player: the player number does not exists");
            }
            else
                Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
        }
于 2013-10-22T02:29:17.117 回答