我是新来的编码,我试图调用数组的索引值。我希望能够说“玩家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");
}
你正在改变'maxWeight'第一,然后测试* *再如果不到你只是改变了它的价值。显然这将是'错误'。你为什么不在同一个'if'子句中使用两个变量(使用大括号{...}')? – UnholySheep
'averageHeight =(totalHeight/weight.Length);'看'weight',应该是'height' – Amy
如果我可以提出建议,'height'或'weight'只有一个字符不同,所以我建议选择一个一个或两个的同义词可以更清楚地区分它们。也许,“质量”代替体重会起作用吗?无论如何,只是一个想法。 – Amy