如何返回值的3个值/替换值

时间:2014-11-08 01:05:16

标签: c#

您好我创建了一个程序,其中包含三个阵列,一个用于姓氏,一个用于得分,一个用于玩家号码,现在我已经完成了所有阵列和一切,但我和#39;我不确定如何从我的delete方法返回3个新数组,以便我可以将它传递给我的playerdelete方法,而且我也不确定如何在if语句中对值进行子化。我也不能使用对象。

任何帮助或指导都将不胜感激

    static Int32[] 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 = 0;
            int index2 = 0;
            int index3 = 0;
            int r = 0;
            int j = 0;
            int t = 0;
            while (index < playerNumbers.Length && index2 < playerLastName.Length && index3 < playerPoints.Length)
            {
                if (index != playerCount) { newArray[r] = playerNumbers[index]; }
                {

                }
                if (0 != 1) { newArray[0] = playerNumbers[0]; }
                {

                }
                if (0 != 1)
                {
                    newArray[0] = 12; //the value of playerNumbers[0]}


                }
                index++;
                if (index2 != playerCount) { newArray2[r] = playerLastName[index2]; }
                {
                }


                if (0 != 1) { newArray2[0] = playerLastName[0]; }
                {

                }
                if (0 != 1)
                {
                    newArray2[0] = null; 
                }
                index2++;
                if (index3 != playerCount) { newArray3[r] = playerPoints[index3]; }
                {

                }
                if (0 != 1) { newArray3[0] = playerPoints[0]; }
                {

                }
                if (0 != 1)
                {
                    newArray3[0] = 12; 


                }
                index3++;
            }
                return newArray;



            }


        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 :(得分:1)

我假设这是作业,否则,“不能使用对象”是没有意义的。

话虽如此,您可以返回一个数组数组,然后只需使用 array_of_arrays[0]来指示第一个等等,或者假设您事先知道了大小,您可以返回一个数组,并做数学自己找到你的索引(要么是所有的第一个数组,然后是所有的第二个,等等,或者其中一个...它看起来像:1,1,1,2,2,2, 3,3,3 ...或1,2,3,1,2,3,1,2,3 ...,其中数字表示该值属于哪个数组)

答案 1 :(得分:0)

你可以尝试只使用一个字符串数组(甚至是一个列表)将每个玩家的值压缩成逗号(或其他char [])分隔的字符串(例如 - &#34; Billy,Barty,42, 12&#34)。然后,每次需要使用它们时,只需将每个字符串(并根据需要进行转换/转换)拆分为实例变量。当您准备删除播放器时,只需从集合中删除一个条目。

List<string> Player = new List<string>
{
    "Billy,Barty,42,12",
    "Jeffy,Jones,12,35",
    "Blueberry,Hill,43,5" // sorry...bad joke reference
}();

然后与玩家合作

static void SomeOtherMethod()
{
    DoSomethingWithPlayer(Player[1]) // Jeffy Jones from the example
}

static void DoSomethingWithPlayer(string player)
{
    string[] p = player.Split((char)','); // create an instance array representing the player
    string firstName = p[0];
    string lastName = p[1];
    int playerNumber = int.TryParse(p[2]);
    int playerPoints = itn.TryParse(p[3]);

    // some code to work with this player or call methods on this player
    // the collection 'p' is an instance that goes away after this method completes
}

如果您需要更新播放器的数据,请使用新数据创建一个新字符串,并替换列表中的现有元素&lt;&gt; (或数组)

string newFirstName = "Blueberry";
string newLastName = "Jones";
string newPlayerNumber = "55";
string newPlayerPoints = "99";

string newPlayerData = newFirstName + "," +
    newLastName + "," +
    newPlayerNumber + "," +
    newPlayerPoints;

Player[2] = newPlayerData

您甚至可以使用这些以逗号分隔的值来读取/写入包含所有播放器数据的.csv文件,而无需客观化;) 它有点难看,但没有对象,你的选择有点受限。