从一个方法传递两个数组以从另一个方法收集用户信息C#

从一个方法传递两个数组以从另一个方法收集用户信息C#,c#,C#,我不是在寻找特定的代码,而是在寻找信息和指导。我想学习,但不想让别人编写代码 我正在研究如何将两个数组传递给不同的方法,以便它们可以被用户输入填充。我似乎无法理解这一点,我研究了各种网站以及我的文本和讲座,但找不到实现这一点所需的技术。我知道如何将一个数组传递给不同的处理方法(即获取平均值/总和等),但不知道如何从一个单独的方法填充两个数组。如有任何指导和信息,将不胜感激。这就是我到目前为止得到的,或者更确切地说,是我剩下的。我把其他方法都做了一遍,只需要把这部分转到调试阶段 using

我不是在寻找特定的代码,而是在寻找信息和指导。我想学习,但不想让别人编写代码

我正在研究如何将两个数组传递给不同的方法,以便它们可以被用户输入填充。我似乎无法理解这一点,我研究了各种网站以及我的文本和讲座,但找不到实现这一点所需的技术。我知道如何将一个数组传递给不同的处理方法(即获取平均值/总和等),但不知道如何从一个单独的方法填充两个数组。如有任何指导和信息,将不胜感激。这就是我到目前为止得到的,或者更确切地说,是我剩下的。我把其他方法都做了一遍,只需要把这部分转到调试阶段

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

namespace PhoneDial
{
    class Program
    {


        // Get player names and their scores and stores them into array for an unknown number of players up to 100
        static void InputData(string[] nameList, int[]playerScore)
        {
            string userInput;
            int count = 0;

            do
            {
                Console.Write("\nEnter a players name: ");
                userInput = Console.ReadLine();
                if (userInput != "Q" && userInput != "q")
                {
                    nameList[0] = Console.ReadLine();
                    ++count;

                }
                else break;

                Console.WriteLine("Enter {0}'s score:", userInput);
                playerScore[0] = Convert.ToInt32(Console.ReadLine());


            } while (userInput != "Q" && userInput != "q");


        }

        //Declare variables for number of players and average score and two arrays of size 100 (one for names, one for respective scores
        //Calls functions in sequence, passing necessary parameters by reference
        //InputData(), passing arrays and number of players variable by reference
        //DisplayPlayerData(), passing arrays and number of players by reference
        //CalculateAverageScore(), passing arrays and number of players by reference. Store returned value in avg variable
        //DisplayBelowAverage(), passing arrays and number of players variable by reference, passing average variable by value
        static void Main(string[] args)
        {
            string[] nameList = new string[100];
            int[] playerScore = new int[100];
            int count = 0, avg = 0;

            InputData(nameList, playerScore);


        }

通过将变量nameList和playerScore放在类程序{下,可以使它们成为全局变量(别忘了将变量设置为静态并列出它们)

然后在InputData中使用.add方法将传统值添加到两个变量中

也许使用字典而不是两个数组也是个好主意


我希望这有助于使用字典而不是数组

Dictionary<string, int> Results = new Dictionary<string, int>();
字典结果=新建字典();

您的问题并不完全清楚,但据我所知,要在一个方法中声明一个数组并传递给另一个要填充的方法,您只需要:

public void MethodA()
{
    string[] stringArray = new string[100];

    MethodB(stringArray);
}

public void MethodB(string[] stringArray)
{
    // Fill the array
    stringArray[0] = "Hello";

    // ...
}
但是,如果希望将某个变量引用传递给某个方法,然后让该方法创建数组并填充它,则需要对数组变量使用
ref
关键字(与标准变量相同)。如下所示:

public void MethodA()
{
    string[] stringArray;

    MethodB(ref stringArray);

    // Array is now created and filled
}

public void MethodB(ref string[] stringArray)
{
    // Create the array
    stringArray = new string[100];

    // Fill the array
    stringArray[0] = "Hello";

    // ...
}
使用两个数组执行这两种方法中的任何一种都是相同的,但添加了一个参数。即:

public void MethodB(string[] array1, int[] array2) { }

public void MethodB(ref string[] array1, ref int[] array2) { }

创建一个包含两个数组的类怎么样?然后你可以随意传递它……也许你不应该使用
静态void InputData(ref string name,ref int score)
你应该使用
静态void InputData(string[]name,int[]score,ref int numberOfPlayers)
创建一个类会把它从main()中删除吗?我们还没有在课堂上讨论字典。代码中的注释是我们必须遵循的方向。:(恐怕这两种方法都不适合我。代码中的逗号是实验室项目的方向。我们还没有讨论字典,如果我没有弄错的话,是不是让它们成为全局的?将它们从Main()中删除)?啊,非常感谢。我想这就是我一直在寻找的。我回家后会尝试一下。我更新了代码以显示你的第一个方法,这是正确的方法吗?另外,做一个方法比另一个方法有什么好处或目的?我想第二个方法会改变数组的值,对吗?使用任何一种方法都可以更改数组中存储的值。但是,第二种方法(使用关键字的方法)通过引用传递数组。这意味着您可以在引用变量时初始化变量,而不仅仅是值。