在数组中调用索引值

时间:2016-11-15 20:06:20

标签: c#

我是编码的新手,我一直试图调用数组的索引值。我想能够说"球员2是最重的,他的体重是72公斤"但我似乎无法获得最大权重数组的索引值。任何帮助都非常感谢,我很抱歉我的代码很乱,但我只是开始学习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");


}

4 个答案:

答案 0 :(得分: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;
    }
}   

答案 1 :(得分: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设置为新的最大重量,然后再次检查最大重量与当前重量。这样,检查发生一次,两个变量都改变了。

答案 2 :(得分:-1)

似乎你将maxWeight设置为该行中该索引处的等权重[i]。

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

你可以立即将i分配给同一个if语句中的另一个变量,或者继续你的第二个if,你想要将它等同于它们现在相等

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

答案 3 :(得分:-1)

如果我是对的,你试图获得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);