证明检查和两个错误 - 从一个方法到另一个方法的未分配变量

时间:2012-02-07 16:16:22

标签: c#

我非常喜欢让人们可以检查工作,无论它是什么。无论是文章,项目,还是只是习惯(图纸),我都喜欢反馈,以帮助我变得更好。下面是我正在为学校工作的项目的代码。我完成了它并且里面有两个错误。错误在Main() - GetInput()内部 - 它为引用变量nameList和playerScore引发了两个错误,并说它们是未分配的。不知道为什么,因为据我所知,我已经分配了它们。但是,任何有关这方面的帮助都会很棒,但我也会更多地寻求反馈,如果我能做得更好或更容易,同时遵循评论中的指示。我必须使用数组,我必须通过方法之间的引用传递它们,同时按值传递avg变量。

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

namespace PhoneDial
{
    class Program
    {
        // Display the player names and corresponding scores
        static void DisplayPlayerData(ref string[] nameList, ref int[] playerScore, ref int count)
        {
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("{0} : {1}", nameList[i], playerScore[i]);
            }
        }

        // Calculate the average score between all players and returns it by value to Main()
        static void CalculateAverageScore(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
        {
            avg = playerScore.Average();

        }

        // Display all players whose score is below the average, with their corresponding scores
        static void DisplayBelowAverage(ref string[] nameList, ref int[] playerScore, ref int count, double avg)
        {
            Console.WriteLine("Players who scored below the average:");
            for (int i = 0; i < count; i++)
            {
                if (playerScore[0] < avg)
                    Console.WriteLine("{0}:{1}", nameList, playerScore);
                count++;
            }
        }

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


            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
        static void Main(string[] args)
        {
            string[] nameList;
            int[] playerScore;
            int count = 0; 
            double avg = 0;

            //InputData(), passing arrays and number of players variable by reference
            //******nameList and playerScore are throwing errors; use of unassigned local variables********
            InputData(ref nameList, ref playerScore, ref count);

            //DisplayPlayerData(), passing arrays and number of players by reference
            DisplayPlayerData(ref nameList, ref playerScore, ref count);

            //CalculateAverageScore(), passing arrays and number of players by reference. Store returned value in avg variable
            CalculateAverageScore(ref nameList, ref playerScore, ref count, avg);

            //DisplayBelowAverage(), passing arrays and number of players variable by reference, passing average variable by value
            DisplayBelowAverage(ref nameList, ref playerScore, ref count, avg);





        }
    }
}

3 个答案:

答案 0 :(得分:1)

从快速浏览一下,您已经使用了ref应该在哪里使用。我认为InputData应该将参数传递给out而不是ref,因为你在那里设置它们。其他方法不会更改这些数组,因此不需要作为ref传递。

答案 1 :(得分:1)

您目前正在使用InputData将变量传递给refref要求您的变量之前已初始化,即在使用string[] nameList = new string[N];变量ref之前必须先调用nameList

当您在nameList = new string[100];方法中撰写InputData时,可以将ref替换为out

static void InputData(out string[] nameList, out int[] playerScore, ref int count)
...
InputData(out nameList, out playerScore, ref count);
  • out不需要初始化,它更像是“我不关心这个变量的先前值,它会被我的方法覆盖,而变量纯粹在这里,因为它是我方法的输出“。
  • refout非常相似,但它更像是“使用变量的值,修改它,并将其作为方法的输出返回”

有关更完整的“ref vs out”说明,请参阅herehere

答案 2 :(得分:1)

这是一个针对您的问题的简单修复,因为您正在使用InputData方法中的字符串[]和int []来修改

        string[] nameList = {}; //in your Main
        int[] playerScore = {}; // in your Main
        int count = 0;

作为动态数组..没有多少开发人员知道你可以做到这一点,但这应该解决你的问题..我只是在我的本地测试这个我自己编译

你也可以宣布它为out但我认为没有必要这样做只要你声明字符串[]并初始化它{}将解决你的问题并且我从来没有任何编码或使用它编程问题..特别是如果你需要调整数组大小而你不知道它需要多大...它快速而干净且相当容易理解..快乐编码..!