.net c#似乎无法修复未分配的局部变量

Can't seem to fix unassigned local variables
2021-09-14
  •  译文(汉语)
  •  原文(英语)

所以我一直在为一个项目编写该程序,一切似乎都正常进行.我一直在检查我的讲师代码,但是我没有前40行代码的副本,无法查明是什么原因导致了这个问题

    static void Main(string[] args)
    {
        int count = 0;
        string[] names = new string[MAX_SIZE];
        int[] scores = new int[MAX_SIZE];
        string[] splitInput = new string[MAX_SIZE];

        int sum = 0;
        int minScore = 0;
        int maxScore = 0;

        string input;

        string minName;
        string maxName;

        Console.WriteLine("===============Saturday Night Coders================");
        Console.WriteLine("===============Bowling Score Program================");

        for (int i = 0; i < MAX_SIZE; i++)
        {




            Console.WriteLine("\n Please Enter a name and a score separated by a space");
            Console.WriteLine("Enter a blank line when finished");

            input = Console.ReadLine();

            if (input == "")

            {
                Console.WriteLine("===========INPUT COMPLETE=========");
                break;
            }





            splitInput = input.Split();

            string name = splitInput[0];
            int score = int.Parse(splitInput[1]);

            names[i] = name;
            scores[i] = score;
            sum += score;




            if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);
        Console.WriteLine("\n The team average was {0:f2}", average);
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
    static void PrintScores(string[] names, int[] scores, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.Write("{0} \t {1}", names[i], scores[i]);
            if (scores[i] == MAX_SCORE)
            {
                Console.WriteLine("*");
            }
            else
            {
                Console.WriteLine("");
            }
            Console.WriteLine();
        }
    }



}

我遇到的问题是这里的这段代码

if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);

未分配的局部变量使用错误是minName和maxName值.如果我用minName =""声明它们;maxName =""; 该代码将编译,但是得分最低的人的名字将是"",得分将是0.这一切似乎一直有效,直到我添加了PrintScores方法.感谢任何帮助,我已经花了一个多小时来摆弄它,但似乎仍然找不到解决方法

解决过程1

您声明minNamemaxName在for循环之外.您只在for循环内分配它们...问题:如果for循环未运行,则不会分配变量.因此,编译器禁止使用它们.

解决方案:只需使用有意义的值初始化它们,例如string.Empty.

So i Have been writing this program for a project and everything seemed to work out fine. I've been checking my work with my instructors code, but I don't have a copy of the top 40 lines of code to check with and I can't figure out what caused this problem

    static void Main(string[] args)
    {
        int count = 0;
        string[] names = new string[MAX_SIZE];
        int[] scores = new int[MAX_SIZE];
        string[] splitInput = new string[MAX_SIZE];

        int sum = 0;
        int minScore = 0;
        int maxScore = 0;

        string input;

        string minName;
        string maxName;

        Console.WriteLine("===============Saturday Night Coders================");
        Console.WriteLine("===============Bowling Score Program================");

        for (int i = 0; i < MAX_SIZE; i++)
        {




            Console.WriteLine("\n Please Enter a name and a score separated by a space");
            Console.WriteLine("Enter a blank line when finished");

            input = Console.ReadLine();

            if (input == "")

            {
                Console.WriteLine("===========INPUT COMPLETE=========");
                break;
            }





            splitInput = input.Split();

            string name = splitInput[0];
            int score = int.Parse(splitInput[1]);

            names[i] = name;
            scores[i] = score;
            sum += score;




            if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);
        Console.WriteLine("\n The team average was {0:f2}", average);
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
    static void PrintScores(string[] names, int[] scores, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.Write("{0} \t {1}", names[i], scores[i]);
            if (scores[i] == MAX_SCORE)
            {
                Console.WriteLine("*");
            }
            else
            {
                Console.WriteLine("");
            }
            Console.WriteLine();
        }
    }



}

the problem I'm having is with this section of code here

if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);

the unassigned use of local variables error is with the minName, and maxName values. if i declare them with minName = ""; maxName = ""; the code will compile but then the name for the person that scored the lowest will be "" and the score will be 0. this all seemed to work until i added the PrintScores method. any help is appreciated, I've been fiddling with it for well over an hour now and still can't seem to figure out a solution

Solutions1

You declare minName and maxName outside your for loop. You only assign them inside the for loop... The problem: If the for loop does not run, the variables are not assigned. Therefor the compiler forbids to use them.

Solution: Simply initialize them with meaningful values, like string.Empty.

转载于:https://stackoverflow.com/questions/15593729/cant-seem-to-fix-unassigned-local-variables

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入