【问题标题】:C# Using iteration to calculate the lowest value in a two dimensional arrayC#使用迭代计算二维数组中的最小值
【发布时间】: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


    【解决方案1】:

    问题是你把所有的数据都存储为String,然后又想以Double的方式操作

    这不是最有效的代码,但它与你的接近,所以我想你会理解它:

                string[,] rainfallData = new string[5, 2];
    
                // Load data for 5 days. All the data is stored as string
                for (int dayOfWeek = 0; dayOfWeek < 5; dayOfWeek++)
                {
                    Console.WriteLine("Please enter a day of the week: ");
                    rainfallData[dayOfWeek, 0] = Console.ReadLine();
    
                    Console.WriteLine($"How many inches of rain did you get on {rainfallData[dayOfWeek, 0]}");
                    rainfallData[dayOfWeek, 1] = Console.ReadLine();
                }
    
    
                // Now let's find the minimum.
                double minRain = Double.Parse(rainfallData[0, 1]);
                for (int dayOfWeek = 1; dayOfWeek < rainfallData.GetLength(0); dayOfWeek++)
                {
                    // you need to convert to double, and then compare.
                    double rainOnDayOfWeek = Double.Parse(rainfallData[dayOfWeek, 1]);
                    if (rainOnDayOfWeek < minRain)
                    {
                        minRain = rainOnDayOfWeek;
                    }
                }
                Console.WriteLine($"The day with the lowest amount of rainfall received {minRain} of rain.");
    

    如果你不明白,请告诉我

    ----编辑----

    仅仅写下为什么你的代码不起作用并不容易解释,我在这里尝试:

                    // Here a lot of very confusing things are happening:
                    // 1. All Strings are an array of characters, so here you are doing: "foreach character in rainfallData[i, 1]"
                    // 2. In all programming languages, every character is also a code (number). That is called ASCII code. 
                    // because you do "double element" you are asking C# to convert that character into it's code
                    // for example, 'a' = 97, 'b' = 98, 'A' = 65, 'B' = 66, '1' = 49, '2' = 50
                    foreach (double element in rainfallData[i, 1])
                    {
                        if (element < myMin2)
                        {
                            myMin2 = element;
                        }
                    }
    
                    // This code is exactly the same as the previous one.
                    // Maybe here you understand better what is going on
                    foreach (char element in rainfallData[i, 1])
                    {
                        // This is not the same as Convert.ToDouble, here you are getting the ASCII code of the char
                        double elementCode = (double)element; 
                        if (element < myMin2)
                        {
                            myMin2 = element;
                        }
                    }
    

    ----编辑 2---- 尝试将此行添加到您的代码中,然后执行它。也许你能明白我的意思:

    Console.WriteLine($"Element = '{element}'. In ASCII, '{element}' == '{(char)element}' and myMin2 = '{myMin2}'");

                double myMin2 = Convert.ToDouble(rainfallData[0, 1]);
                for (int i = 0; i < rainfallData.GetLength(0); i++)
                {
                    foreach (double element in rainfallData[i, 1])
                    {
                        Console.WriteLine($"Element = '{element}'. In ASCII, '{element}' == '{(char)element}' and myMin2 = '{myMin2}'");
                        if (element < myMin2)
                        {
                            myMin2 = element;
                        }
                    }
    

    【讨论】:

    • 首先,谢谢!我对 C# 还是很陌生,但是 Convert.ToDouble 和你使用的 Double.Parse 有什么区别?
    • 不客气!对于您的情况,没有区别。但是为了将来参考,有一些区别:例如,Double.Parse 允许您指定文化是什么(例如,如果十进制符号是点或逗号)
    • 我将其标记为已回答。我不知道您可以按照自己的方式填充数组的元素。我想我们还没有走到那一步——这只是我们必须编写的第三个程序。话虽如此,任务说要创建一个字符串数组,要求用户输入星期几和每天的降雨量,然后在创建数组后做一些事情(总和,平均,找到最低,找到最高的)。当您使用 Double.Parse 将字符串转换为双精度时,我使用的是 Convert.ToDouble。这是我遇到麻烦的地方吗?
    • 不,问题是您不需要 foreach。相反,您需要将雨量数据 [i, 1] 中的值转换为两倍。我编辑了答案,试图更多地解释你的代码有什么问题。每次我使用 Double.Parse 时,您都可以使用 Convert.ToDouble 并且可以正常工作,这不是您的代码的问题
    【解决方案2】:
    string[,] rainfallData = new string[5, 2];
            int b = 0;
            int lowest = 99,highest=0;
            for (int i = 0; i <rainfallData.Length-5; i++)
            {
    
                for (b = 0; b < 1; b++)
                {
    
                    if (i != rainfallData.Length)
                    {
                        Console.Write("Please enter a day of the week: ");
                        rainfallData[i, b] = Console.ReadLine();
                        Console.Write("How many inches of rain did you get on {0}: ", rainfallData[i, b]);
                        rainfallData[i, b] = Console.ReadLine();
                    }
                    else
                        break;
    
                }
    
            }
            for (int i = 0; i < rainfallData.Length-5; i++)
            {
                for (b = 0; b<1; b++)
                {
                    lowest = Math.Min(lowest, Convert.ToInt32(rainfallData[i, b]));
                    highest = Math.Max(highest, Convert.ToInt32(rainfallData[i, b]));
    
                }
    
            }
    
            Console.WriteLine("The day with the lowest amount of rainfall received {0}\" of rain.", lowest);
            Console.WriteLine("The day with the highest amount of rainfall received {0}\" of rain.", highest);
    

    【讨论】:

    • 感谢您的回答维克多。您正在将一周中的某一天与降雨量进行比较。例如,rainbowData[0] = "Wednesday" 和雨量数据 [1] = 14.8 .... 如果您删除第二个嵌套的 for,那么您的代码将完美运行 :)
    • 感谢卡洛斯的许可。我现在才注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2019-01-01
    • 2023-02-06
    • 2020-06-20
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多