我是新來的編碼,我試圖調用數組的索引值。我希望能夠說「玩家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