如何从用户输入的数组中删除数组元素

时间:2014-11-09 20:27:49

标签: c#

我想创建一个包含三个阵列的菜单驱动程序:一个用于人的姓,一个用于得分,一个用于玩家号。我做了一个deletemethod和deleteplayer方法,假设当用户输入一个玩家编号时,删除玩家编号,姓氏和用户输入列表中的点数,但是当我使用删除方法时,它会清除整个列表。我不确定如何解决这个问题。我还必须将程序保留在主类中。

任何帮助都将不胜感激,谢谢

    static void ProcessDelete( Int32[] playerNumbers, ref Int32 playerCount,  String[] playerLastName,  Int32[] playerPoints)
        {

            Int32[] newArray = new Int32[playerNumbers.Length]; String[] newArray2 = new String[playerLastName.Length]; Int32[] newArray3 = new Int32[playerPoints.Length];

            int index = Array.IndexOf(playerNumbers, 0);


            {
                for (int i = 0; i < playerNumbers.Length; i++)
                    playerNumbers[i] = 0;
            }

            // String[] playerLastName = new String[] { null, null, null };


            for (int i = 0; index < playerLastName.Length; index++)

                    playerLastName[i] = " ";

            for (int i = 0; i < 10; i++)
            {
                //Console.WriteLine(newArray2[i]);
            }

            for (int i = 0; i < playerPoints.Length; i++)
                playerPoints[i] = 0;
        }

        static void DeletePlayer(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
            int player;// Player number to delete
            int playerindex;//index of the player number in Array
            if (playerCount < MAXPLAYERS)
            {

                player = GetPositiveInteger("\nDelete Player: please enter the player's number");
                playerindex = GetPlayerIndex(player, playerNumbers, playerCount);


               if (playerindex != -1)
                {

                    {

                        Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastName[playerindex], playerPoints[playerindex]);
                        Console.WriteLine("Succesfully Deleted");
                        Console.WriteLine();
                        ProcessDelete( playerNumbers, ref playerCount,  playerLastName, playerPoints);
                    }
                }
                else
                    Console.WriteLine("\nDelete Player: player not found");
            }
            else
                Console.WriteLine("\nDelete Player: the roster is empty");
        }

    }
}

2 个答案:

答案 0 :(得分:0)

如果列表中的所有信息都与逻辑相关,为什么您不使用列表或词典?

这样的事情:

using System.Collections.Generic;

class Player{
    public int Number;
    public string LastName;
    public int Points;
}

class PlayerManager{
    static Dictionary<int,Player> dctOfPlayers= new Dictionary<int, Player>();

    public static AddNewPlayer(Player newPlayer){
        dctOfPlayers.Add(newPlayer.Number,newPlayer);
    }

    public static RemovePlayer(int playerNumber){
        dctOfPlayers.Remove(playerNumber);
    }
}

您可以使用播放器列表并制作循环来查找用户ID ......

此外,如果您因任何原因想要保留播放器条目并重置信息,那么您可以:

using System.Collections.Generic;

class Player{
    public int Number;
    public string LastName;
    public int Points;

    public ResetInfo(){
        Number=0;
        LastName="";
        Points=0;
    }
}

class PlayerManager{
    static Dictionary<int,Player> dctOfPlayers= new Dictionary<int, Player>();

    public static AddNewPlayer(Player newPlayer){
        dctOfPlayers.Add(newPlayer.Number,newPlayer);
    }

    public static RemovePlayer(int playerNumber){
        dctOfPlayers[layerNumber].ResetInfo();
    }
}

答案 1 :(得分:0)

在面向对象语言中,通常会创建一个类来捕获此信息。 如果您需要任何解释,请告诉我,因为一如既往,有多种方法可以做到这一点。

更新了CurrentScore,AddPlayerToGame()函数,并增加了playercount

class Program {
    static void Main(string[] args) {
        //Create firstPlayer
        Player firstPlayer = new Player {
            PlayerId = 1,
            DisplayName = "Goober",
            LastName = "Smith"
        };

        // Create secondPlayer
        Player secondPlayer = new Player {
            PlayerId = 2,
            DisplayName = "Destructor",
            LastName = "Henry"
        };

        // Create game instance
        Game currentGame = new Game();
        // Add players to game
        currentGame.AddPlayerToGame(firstPlayer);
        currentGame.AddPlayerToGame(secondPlayer);

        // Player scores a point
        secondPlayer.CurrentScore++;

        // Player clicks LeaveGame, etc.
        currentGame.PlayerLeavesGame(firstPlayer);
    }
}


public class Player {
    public int PlayerId { get; set; } // I assume the same as player numbers
    public string DisplayName { get; set; }
    public string LastName { get; set; }
    public int CurrentScore { get; set; }
}

public class Game {
    private static readonly int MAXPLAYERS = 10;
    public Game() {
        Players = new List<Player>();
    }
    public List<Player> Players { get; private set; }

    private int _PlayerCount = 0;
    public int PlayerCount {
        get {
            return _PlayerCount;
        }
        set {
            _PlayerCount = value;
        }
    }

    /// <summary>
    /// Tries to add a player to the game
    /// </summary>
    /// <param name="playerThatJoinedGame"></param>
    /// <returns>True if the player was added and the game wasn't full</returns>
    public bool AddPlayerToGame(Player playerThatJoinedGame) {
        if (PlayerCount < MAXPLAYERS) {
            Players.Add(playerThatJoinedGame);
            PlayerCount++;
            return true;
        }
        else {
            return false;
        }


    }
    /// <summary>
    /// Removes a player from the game
    /// </summary>
    /// <param name="playerThatQuit">The leaving player</param>
    public void PlayerLeavesGame(Player playerThatQuit) {
        // Removes the player from the game
        Players.Remove(playerThatQuit);
        PlayerCount--;
    }
}