1

だから私はプロジェクトのためにこのプログラムを書いていて、すべてがうまくいくようでした。インストラクターのコードを使用して作業をチェックしていますが、チェックするコードの上位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時間以上それをいじっています、そしてそれでも解決策を理解することができないようです

4

1 に答える 1

2

forループの外側で宣言minNameします。maxNameそれらはforループ内でのみ割り当てます...問題:forループが実行されない場合、変数は割り当てられません。そのため、コンパイラはそれらの使用を禁止しています。

解決策:のような意味のある値でそれらを初期化するだけstring.Emptyです。

于 2013-03-24T00:38:20.377 に答える