2012-02-07 5 views
1

私は、それが何であっても仕事を確認できる人を持つことの大きなファンです。エッセイであろうとプロジェクトであろうと、習慣(絵)であろうと、私はより良いものになるためのフィードバックが好きです。以下は、私が学校で働いていたプロジェクトのコードです。私はそれをかなり終えたし、そこに2つのエラーがある。エラーはMain() - GetInput()の内部にあります。参照変数nameListとplayerScoreの2つのエラーを投げていて、割り当てられていないと言っています。私は彼らが私の知る限りで割り当てられているので、なぜ、なぜわからない。しかし、それを助けてくれれば助かりますが、コメントの指示に従って、何か良いやり方をすることができるかどうかのフィードバックをもっと探しています。私は配列を使用する必要があり、値ごとにavg変数を渡している間にメソッド間の参照によってそれらを渡す必要があります。あなたが出て使用しなければならないREFを使用しているチラッからプルーフ・チェックと2つのエラー - あるメソッドから別のメソッドへの割り当てられていない変数

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); 





     } 
    } 
} 

答えて

1

現在、変数をInputDatarefを使用して渡しています。 refは、変数nameListrefを使用する前に、変数を初期化する必要があります。つまり、string[] nameList = new string[N];を呼び出す必要があります。

あなたInputData方法でnameList = new string[100];を書いたように、あなたはoutrefを置き換えることができます:

static void InputData(out string[] nameList, out int[] playerScore, ref int count) 
... 
InputData(out nameList, out playerScore, ref count); 
  • outが初期化を必要としない、それは私が以前の値を気にしないより」のようなものですこの変数は、私のメソッドで上書きされます。変数は、私のメソッドの出力であるため、純粋にここにあります。
  • refoutに非常によく似ているが、それはより多くのようなものだ「それを修正し、変数の値を使って、そして私の方法の出力として返す」

「より完全なためherehereを参照してください。 refout "説明。あなたは[]の文字列NEWINGであり、intは[]あなたの入力データの方法であなたが

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

として動的配列をdecareすることができますので、ここで

+0

コード内のすべてのコメント(私のエラーコメントを除く)は、このラボで提供された疑似コードから直接得られたものです。これが私が参考ルートを試していた理由です。私が家に帰るとき、私は間違いなくこれを裂くでしょう。 – user1174357

+0

Ken2kのように初期のエラーを修正しました.Outは初期化を必要としませんが、文字列[]またはint []を初期化したい場合は、空の初期化子{}を使うことができます。あなたがそれを宣言したい最初の長さを知らないときにも.. Resize(ref ...)からあなたを離れて常に新しいものにしています。それはちょうど私の好みです..あなたの研究室の仕事で幸運 – MethodMan

1

。私はあなたがそこにそれらを設定しているので、InputDataがrefでないとしてパラメータを渡すべきだと思います。他のメソッドはこれらの配列を変更しないので、refとして渡す必要はありません。

+0

http://msdn.microsoft.com/en-us/library/szasx730(v=vs.100).aspxを参照してください –

+0

返信いただきありがとうございます、私は仕事から降りるとき、私はdefinatlyこれをチェックします家に帰る。 :Dコメントは、ラボで提供された擬似コードから直接取得されます。興味がある、私はラップトップに帰るときにそれを確認する必要があります。 – user1174357

1

は...ない多くの開発者が認識している問題を修正する簡単な方法ですあなたはこれを行うことができますが、これはあなたの問題を解決する必要があります..私はちょうど私の地元の私の自己でこれをテストし、それがコンパイルされた

あなたもそれを宣言している可能性がありますが、文字列[]を宣言して問題を解決する{}を初期化すると、それを使用したコーディングやプログラミングの問題は一度もありませんでした。配列とあなたはそれがどれくらい大きくなる必要があるかわからない..それは迅速かつきれいで、かなり理解しやすい。幸せなコーディング..!

+0

それは新しい方法です。私は{}で前にそれを見たことがありますが、それは同時に{5、6、7、8}と同じ時間にそれを埋めていたときです。C#で配列のサイズを宣言しなければならないという印象を受けました。 – user1174357

+0

これは動的配列と呼ばれます。多くの開発者がこの構造を認識していません。 – MethodMan

関連する問題