2012-02-20 3 views
-2

私はユーザーから10個の数字を取り出し、合計して平均を加算するコンソールプログラムを作ろうとしています。 do whileループの中で、プログラムは次の番号を尋ね続けます。C# - 配列の問題。ユーザー入力を読まない

{ 
     Console.WriteLine("Hey there! If you could go ahead and just give me like 10 numbers, that'd be great... And I'll tell you what, if you do, I'll add them up and average them all up for ya."); 

     // declare an array of strings 
     int[] aryNumbers; 
     int intSum = 0; 
     int intAverage = 0; 

     // initialize the array 
     aryNumbers = new int[10]; 

     aryNumbers[0] = int.Parse(Console.ReadLine()); 
     aryNumbers[1] = int.Parse(Console.ReadLine()); 
     aryNumbers[2] = int.Parse(Console.ReadLine()); 
     aryNumbers[3] = int.Parse(Console.ReadLine()); 
     aryNumbers[4] = int.Parse(Console.ReadLine()); 
     aryNumbers[5] = int.Parse(Console.ReadLine()); 
     aryNumbers[6] = int.Parse(Console.ReadLine()); 
     aryNumbers[7] = int.Parse(Console.ReadLine()); 
     aryNumbers[8] = int.Parse(Console.ReadLine()); 
     aryNumbers[9] = int.Parse(Console.ReadLine()); 

     do 
     { 
      Console.WriteLine("Okay, give me a number."); 
      aryNumbers[] = int.Parse(Console.ReadLine()); 

     } while (intSum != 0); 


     int intNumbers = aryNumbers.Length; 
     //for loop to average sum of array elements 
     for (int i = 0; i < intNumbers; i++) 
     { 
      intSum += aryNumbers[i]; 
     } 

     intAverage = intSum/intNumbers; 
     Console.WriteLine("You're average comes out to... " + intAverage); 

     Console.ReadKey(); 
    } 
} 

私は本当に何をすべきか見当もつかない、私はこの

おかげでここ

+6

このコードは正しくコンパイルされますか? do/whileループ内のコードの中には疑わしいものがあります。 (また、この宿題/教育ですか?) – reuben

+0

、申し訳ありません。それは正しくコンパイルされていません。エラーは、doの中に "Identifier expected"と表示され、等号でループします。はい、それはクラスのためのものです。私は必ずしも答えを探しているわけではありませんが、ちょうど正しい道に乗るための助けとなります。 – user1207424

+1

これは良いヒントです。配列内の要素にアクセスするための構文を確認する必要があります。そうするときは、明示的なインデックスを提供する必要があります。 – reuben

答えて

1

に非常に新しいですが、私のコードです:

 using System.Linq; 

     ..... 

     Console.WriteLine("Hey there! If you could go ahead and just give me like 10 numbers, that'd be great... And I'll tell you what, if you do, I'll add them up and average them all up for ya."); 
     // declare the array 
     int[] aryNumbers = new int[10]; 

     for(int i =0; i<aryNumbers.Length;i++) 
     { 
      Console.WriteLine("Okay, give me a number."); 
      aryNumbers[i] = int.Parse(Console.ReadLine()); 
     } 


     int intAverage = (int)aryNumbers.Average(); 
     Console.WriteLine("You're average comes out to... " + intAverage); 

     Console.ReadKey();   
+6

私たちは、特に宿題のときに、ここに答えを与えないようにしています。おそらく(うまくいけば?)営業担当者は、割り当てを行うだけでなく、これを自分自身でやる方法を学びたいと考えています。 –

+0

+1はLINQをうまく使用します。しかし、ポスターのコードサンプルにも当てはまる2つのスタイルの批判があります。「配列を宣言する」という明白なコメントは必要ありません。ハンガリー語を使用する場合は、配列に標準の "rg"接頭辞を使用します。 "ary。" –

+1

教育/宿題を目的としたこの質問は、完成したソリューションを提供するだけではあまり役に立ちません。 reuben

3

には多くの問題があり、あなたのコード。私はあなたが配列の全章を読むべきだと思います。 HereさんのMSDNチュートリアルです。

+0

ありがとうございます。残念ながら、クラスの教科書はありません。完全なメモを取っていないと、私たちはうんざりです。再度、感謝します! – user1207424

+0

@ user1207424:Googleの教科書! – atoMerz

関連する問題