0

大家好,我创建了一个程序,允许我创建、删除、更新、列出和检索三个数组的用户输入现在我的更新功能出现问题,我无法获得打印正确文本的功能而不是说它Update Player: Player [player number] has a point total of [number of points] pointsUpdate Player: Player System.int32[] has a point total of System.int32[] points

有谁知道如何让它工作我在想我需要将它存储到一个新变量中,并且只需将新变量用于更新函数,但每次我尝试它时都会得到相同的结果,因为新变量和旧的是一样的,当旧的改变时,新的也会效仿

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

namespace FinalArrays
{
    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' 
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'U' && menuItem !='D')
            {
                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': 
                    ProcessRetrieve(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS);
                    break;
                case 'U':
                    ProcessUpdate(playerNumbers,playerLastNames,playerPoints, playerCount,MAXPLAYERS);
                    break;
                case 'D':
                    ProcessDelete(playerNumbers, playerLastNames, playerPoints, playerCount, ref 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++)
                    if (playerLastNames[player].ToString().Length != 0)
                    {
                        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, 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"); 
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
                if (playerindex != -1)
                {
                  //  if (playerLastNames[playerindex].ToString().Length!= 0)
                    {
                        //Console.WriteLine("{0,7}{1,-25}{2,6}", playerNumbers[playerindex], playerLastNames[playerindex], playerPoints[playerindex]);
                        Console.WriteLine("\nRetrive Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastNames[playerindex], playerPoints[playerindex]);
                        Console.WriteLine();
                    }
                }
                else
                    Console.WriteLine("\nRetrieve Player: player not found");
            }
            else
                Console.WriteLine("\nRetrieve Player: the roster is empty");

        }

这是我遇到问题的更新功能

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

            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)
                {
                    Console.WriteLine("\nUpdate Player: Player {0} has a point total of {1} points",playerNumbers,playerpoints);
                    points = GetPositiveInteger("\nUpdate Player: please enter player's new point total");
                    Console.WriteLine("\nUpdate Player: Number - {0}, Name - {1}, Points - {2}, successfully updated", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]);



                    playerpoints[playerindex] = points;

                }
                else
                    Console.WriteLine("\nUpdate Player: player not found");
            }
            else
                Console.WriteLine("\nUpdate Player: the roster is empty");
        }

            static void ProcessDelete(Int32[] playerNumbers, string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, ref Int32 playerCount,  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("\nDelete Player: please enter the player's number");
                    playerindex = GetPlayerIndex(player, playerNumbers, playerCounts);

                    if (playerindex != -1)
                    {

                        Console.WriteLine("The player entry was sucessfully deleted");
                        Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}, deleted successfully", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]);
                        lastName = "";
                        points = 0;
                        playerNumbers[playerindex] = 0;
                        playerLastnames[playerindex] = lastName;
                        playerpoints[playerindex] = points;
                    }
                    else
                        Console.WriteLine("\nDelete Player: player not found");
                }
                else
                    Console.WriteLine("\nDelete Player: the roster is empty");
            }
        }
    }    
4

3 回答 3

1

您正在尝试打印出整个数组。将其更改为打印出 playerNumbers[playerindex] 而不仅仅是 playerNumbers。

于 2013-10-30T20:07:50.090 回答
1

你没有索引数组。您的 WriteLine 语句应该是:

Console.WriteLine("\nUpdate Player: Player {0} has a point total of {1} points",playerNumbers[playerIndex], playerpoints[playerIndex]);
于 2013-10-30T20:08:39.907 回答
1

我会将其存储在玩家属性列表中,而不是 3 个单独的数组中。以您的方式与他们打交道会非常痛苦。

class Player
{
    int number;
    int score;
    string lastName;
}
var players = new List<Player>();
于 2013-10-30T20:20:30.187 回答