2016-11-15 34 views
0

我是新來的編碼,我試圖調用數組的索引值。我希望能夠說「玩家2是最重的,他的體重是72kg」,但我似乎無法獲得最大重量陣列的指標值。任何幫助非常感謝,我很抱歉我的代碼是一團糟,但我只是開始學習c sharp。調用數組中的索引值

{ 
    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'只有一個字符不同,所以我建議選擇一個一個或兩個的同義詞可以更清楚地區分它們。也許,「質量」代替體重會起作用嗎?無論如何,只是一個想法。 – Amy

回答

-1

看來你設置maxWeight等於這個指標在這一行的權重[我]。

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

if語句,您可以立即指派我到另一個變量相同,或與你的第二個,如果繼續,你想等同起來,因爲他們現在是等於

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

這將很可能會產生錯誤的結果,由於浮動/雙重不準確 – UnholySheep

0

您問題在於你正在覆蓋maxWeight和maxHeight,因此第二個if子句將始終爲假。

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

您的歡迎。不要忘記糾正艾米發現的錯誤。 (請參閱您的問題底下的評論) –

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[]指數值?你不必做for loop來檢查和過濾一個數組元素。

爲了讓您的陣列的最大值,您可以使用該方法.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);