2016-11-15 72 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);