2016-11-15 10 views
0

コーディングが新しく、配列のインデックス値を呼び出そうとしています。私は「プレーヤー2が最も重く、体重が72kgだ」と言いたいのですが、最大の体重アレイの指標値を得ることはできません。どんな助けでも大変ありがとうございます。申し訳ありませんが、私のコードは混乱していますが、私は鋭いこと​​を学び始めました。配列内のインデックス値を呼び出す

{ 
    double[] weight; 
    double[] height; 
    double totalHeight = 0; 
    double totalWeight = 0; 
    double averageHeight = 0; 
    double averageWeight = 0; 
    double maxWeightIndex =0; 
    double maxHeightIndex =0; 

    weight = new double [5] { 0, 0, 0, 0, 0}; 
    double maxWeight = weight[0]; 

    height = new double [5] { 0, 0, 0, 0, 0}; 
    double maxHeight = weight[0]; 

    for (int i = 0; i < weight.Length ; i++) 
    { 
     Console.WriteLine("What is the weight of player " + (i+1)); //asking user to what the weight of a player is 
     weight[i] = Convert.ToInt32(Console.ReadLine()); 

     Console.WriteLine("What is the height of player " + (i+1));  //asking user to what the height of a player is 
     height[i]= Convert.ToInt32(Console.ReadLine()); 

     totalHeight += height[i];    // total height 
     totalWeight += weight[i];    // total weight 

     averageHeight = (totalHeight/ weight.Length);  //average height 
     averageWeight = (totalWeight/ weight.Length);  //average weight 
    } 

    for (int i = 0; i < weight.Length ; i++)  
    { 
     if (maxWeight < weight[i]) maxWeight = weight[i];    //max value of weight 
     if (maxHeight < height[i]) maxHeight = height[i];    // max value of height 



     if (maxWeight < weight[i]) maxWeightIndex = i;     //attempt at getting max weight index value 

     if (maxHeight < height[i]) maxHeightIndex = i;     //attempt at getting max height index value 

    } 

    Console.WriteLine("The total weight of the team is " + totalWeight + "kg's"); 
    Console.WriteLine("The total height of the team is " + totalHeight + "cm's"); 
    Console.WriteLine("The average height of the team is " + averageHeight + "cm's"); 
    Console.WriteLine("The average weight of the team is " + averageWeight + "kg's"); 
    Console.WriteLine("Player " + maxWeightIndex + " is the heaviest player and he weighs " + maxWeight + "kg's"); 
    Console.WriteLine("Player " + maxHeightIndex + " is the tallest player and he is " + maxHeight + "cm's"); 


} 
+0

あなたはあなただけにそれを変更した値よりもそれが少ない場合maxWeight'は最初にして*再び*テスト '変化しているが。明らかにこれは「偽」です。なぜ、同じ 'if'節の中で両方の変数を割り当てていないのですか(中括弧' {...} 'を使用します)? – UnholySheep

+1

'averageHeight =(totalHeight/weight.Length);' 'weight'を参照してください.'height' – Amy

+0

私が提案をすることができれば、' height'または 'weight'は1文字だけが異なるので、一方または両方の同義語を使用して、より明確に区別することができます。重量の代わりに「質量」が働くでしょうか?とにかく、ちょっと考えました。 – Amy

答えて

-1

maxWeightは、この行のそのインデックスで同じ重み[i]に設定されているようです。彼らは今、等しくなるよう

if (maxWeight < weight[i]) maxWeight = weight[i]; 

あなたはif文のいずれか、すぐに同じで別の変数iに割り当てることができる場合、または2番目を進めるために、あなたはそれを同一視するのでしょう

if (maxWeight == weight[i]) maxWeightIndex = i; 
+0

これはフロート/ダブルの不正確さのために間違った結果をもたらすでしょう – UnholySheep

0

あなた問題はmaxWeightとmaxHeightを上書きしていることです。したがって、2番目のif節は常にfalseになります。

for (int i = 0; i < weight.Length ; i++)  
{ 
    if (maxWeight < weight[i]) maxWeight = weight[i]; 
    if (maxHeight < height[i]) maxHeight = height[i]; 
    // maxWeight == weight[i] here so the result is false. 
    if (maxWeight < weight[i]) maxWeightIndex = i; 
    if (maxHeight < height[i]) maxHeightIndex = i; 
} 

よりよい:

for (int i = 0; i < weight.Length ; i++)  
{ 
    if (maxWeight < weight[i]){ 
     maxWeight = weight[i]; 
     maxWeightIndex = i; 
    } 
    if (maxHeight < height[i]){ 
     maxHeight = height[i]; 
     maxHeightIndex = i; 
    } 
} 
+0

これは私が過ごしてくれてありがとう最後の2時間はこれを解決しようとしています – paul

+0

ようこそ。 Amyが見つけたエラーも訂正することを忘れないでください。 (あなたの質問の下に彼女のコメントを参照してください) –

0

変更

for (int i = 0; i < weight.Length ; i++)  
{ 
    if (maxWeight < weight[i]) maxWeight = weight[i];    //max value of weight 
    if (maxHeight < height[i]) maxHeight = height[i];    // max value of height 



    if (maxWeight < weight[i]) maxWeightIndex = i;     //attempt at getting max weight index value 

    if (maxHeight < height[i]) maxHeightIndex = i;     //attempt at getting max height index value 

} 

for (int i = 0; i < weight.Length ; i++)  
    { 
     if (maxWeight < weight[i]) 
     { 
      maxWeight = weight[i];    //max value of weight 
      maxWeightIndex = i;     //attempt at getting max weight index value 
     } 
     if (maxHeight < height[i]) 
     { 
      maxHeight = height[i];    // max value of height 
      maxHeightIndex = i;     //attempt at getting max height index value 
     }     
    } 

の問題は、あなたが新しい最大重量にmaxWeightを設定し、次にに対する最大重量をチェックしているあります現在のウェイトを再度設定します。このように、チェックは一度起こり、両方の変数が変更されます。

0

私が正しい場合、最大値height[]と最大値weight[]のインデックス値の両方を取得しようとしていますか?

を実行して、配列要素を確認してフィルタリングする必要はありません。

あなたはメソッド.max()使用することができ、あなたの配列の最大値を取得するには:

double maxWeight = weight.max(); 
double maxHeight = height.max(); 

// then you can use an `.ToList().IndexOf(value)` to get the index. 
int maxWeightIndex = weight.ToList().IndexOf(maxWeight); 
int maxHeightIndex = height.ToList().IndexOf(maxHeight); 
関連する問題