2013-03-24 48 views
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方法。任何帮助表示赞赏,我一直摆弄它已经有一个多小时的时间了,但仍然无法找出解决方案

回答

2

您在for循环之外声明minNamemaxName。你只在for循环内部赋值它们...问题:如果for循环没有运行,变量不会被赋值。因此,编译器禁止使用它们。

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

+0

谢谢你的工作 – 2013-03-24 22:01:19