2013-03-24 51 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