【发布时间】:2019-09-22 21:30:25
【问题描述】:
我刚开始我的第一个 C# 编程课(完全没有编程经验),我必须要求用户输入 5 天内以及每一天的降雨量,并将数据存储在二维字符串数组中命名为雨量数据。然后我需要找到所有使用迭代输入的总和、平均值、最低和最高值。我已经算出总和和平均值,但我不知道如何找到最低值和值。我知道如何找到一维数组中的最小值和最大值,但无法计算出在多维数组中找到它所需的迭代。
string[,] rainfallData = new string[5, 2];
Console.Write("\nPlease enter a day of the week: ");
rainfallData[0, 0] = Console.ReadLine();
Console.Write("\nHow many inches of rain did you get on {0}: ", rainfallData[0, 0]);
rainfallData[0, 1] = Console.ReadLine();
Console.Write("\n\nPlease enter the next day of the week: ");
rainfallData[1, 0] = Console.ReadLine();
Console.Write("\nHow many inches of rain did you get on {0}: ", rainfallData[1, 0]);
rainfallData[1, 1] = Console.ReadLine();
Console.Write("\n\nPlease enter the next day of the week: ");
rainfallData[2, 0] = Console.ReadLine();
Console.Write("\nHow many inches of rain did you get on {0}: ", rainfallData[2, 0]);
rainfallData[2, 1] = Console.ReadLine();
Console.Write("\n\nPlease enter the next day of the week: ");
rainfallData[3, 0] = Console.ReadLine();
Console.Write("\nHow many inches of rain did you get on {0}: ", rainfallData[3, 0]);
rainfallData[3, 1] = Console.ReadLine();
Console.Write("\n\nPlease enter the next day of the week: ");
rainfallData[4, 0] = Console.ReadLine();
Console.Write("\nHow many inches of rain did you get on {0}: ", rainfallData[4, 0]);
rainfallData[4, 1] = Console.ReadLine();
double myMin2 = Convert.ToDouble(rainfallData[0, 1]);
for (int i = 0; i < rainfallData.GetLength(0); i++)
{
foreach (double element in rainfallData[i, 1])
{
if (element < myMin2)
{
myMin2 = element;
}
}
}
Console.WriteLine("The day with the lowest amount of rainfall received {0}\" of rain.\n", myMin2);
无论为雨量输入什么值,myMin2 始终是雨量数据[0,1] 的值。我知道这是因为我设置了 myMin2 = Convert.ToDouble(rainfallData[0, 1] 但是在代码前面找到一维数组的最小值时,这种相同的格式有效。循环让我陷入循环和我已经在这一节卡了3个小时了。我想自己找到答案,但我怕此时见林不见林,浪费了宝贵的时间,我现在已经不在了.
PS - 我已尽力正确发布所有内容,但这是我第一次在此网站上发布,我还不知道所有规则。如果我做错了,我很抱歉。
【问题讨论】:
标签: c# arrays multidimensional-array iteration