C# 在数组中调用索引值

C# 在数组中调用索引值,c#,C#,我不熟悉编码,我一直在尝试调用数组的索引值。我想说“玩家2最重,体重72公斤”,但我似乎无法得到最大重量数组的索引值。非常感谢您的帮助,很抱歉我的代码乱七八糟,但我才刚刚开始学习c语言 { double[] weight; double[] height; double totalHeight = 0; double totalWeight = 0; double averageHeight = 0; double averageWeight =

我不熟悉编码,我一直在尝试调用数组的索引值。我想说“玩家2最重,体重72公斤”,但我似乎无法得到最大重量数组的索引值。非常感谢您的帮助,很抱歉我的代码乱七八糟,但我才刚刚开始学习c语言

{
    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;
双倍总重量=0;
双平均高度=0;
双平均权重=0;
double maxWeightIndex=0;
双maxHeightIndex=0;
权重=新的双精度[5]{0,0,0,0,0};
双倍最大重量=重量[0];
高度=新的双精度[5]{0,0,0,0,0};
双倍最大高度=重量[0];
对于(int i=0;i
您似乎在此行的索引中将maxWeight设置为等于权重[i]

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

您的问题是您正在覆盖maxWeight和maxHeight,因此第二个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
更好:

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;
    }
}   
for(int i=0;i
更改

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

for(int i=0;i

问题是您正在将maxWeight设置为新的最大重量,然后再次根据当前重量检查最大重量。这样,检查只进行一次,并且两个变量都更改。

如果我是对的,您正在尝试获取
高度[]的最大值。
重量[]的最大值。
索引值

不必对循环执行
检查和筛选数组元素

要获得数组的最大值,可以使用方法
.max()


首先更改
maxWeight
,然后再次测试它是否小于刚才更改的值。显然,这将是
false
。为什么不在同一个
if
子句(使用大括号
{…}
)中分配两个变量?
averageHeight=(totalHeight/weight.Length)
参见
weight
,应该是
height
,如果我可以提出建议,
height
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

} 
  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
        }                  
    }  
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);