简体   繁体   English

如何以双格式C#查找2D字符串数组的计算?

[英]How to find calculations of 2D string arrays in double format C#?

I have a Console Application in C# that allows a user to input 5 days of the week and then 5 numerical values corresponding with the days in a single 2D array. 我在C#中有一个控制台应用程序,它允许用户输入一周中的5天,然后在单个2D数组中输入与天数对应的5个数值。

The array is in string format so I tried to Parse the second half of the string data and produce a sum of them but it says my index is out of bounds, any ideas? 该数组为字符串格式,因此我尝试解析字符串数据的后半部分并产生它们的总和,但是它表示我的索引超出范围,有什么想法吗?

Code posted below: 代码如下:

//Ask the user to enter five days of the week and rainfall data for each day

double rainsum = 0;

Console.WriteLine("Please enter 5 days of the week.");

//Store the data in a two dimensional string array named rainfallData[]
for (int i = 0; i < 2; i++)
{
  for (int j = 0; j < 5; j++)
  {
    rainfallData[i, j] = Console.ReadLine();
  }
  if (i == 0)
  {
    Console.WriteLine("Please enter the corresponding rain data.");
  }
}

Console.WriteLine("Data placed in raindallData[] array.");

for (int i = 0; i < 2; i++)
{
  Console.WriteLine();
  for (int j = 0; j < 5; j++)
  {
    Console.WriteLine("rainfallData({0},{1})={2}", i, j, rainfallData[i, j]);
  }
}

//Use iteration to calculate the following from the values in rainfallData[]:
//a) sum
Console.Write("Data values calculated using iteration. \n a) Sum of rainfallData[] = ");
for (int i = 1; i < rainfallData.Length; i++)
{
  Console.WriteLine();
  for (int j = 0; j < 5; j++)
  {
    rainsum += double.Parse(rainfallData[i, j]);
  }
}
Console.WriteLine(rainsum);

//End Program
Console.ReadKey(true);

Your problem is that rainfallData.Length refers to the number of items in the 2d array - in your case this is 2 x 5 = 10 , but you were putting that index in the first dimension and causing the out of bound error. 您的问题是rainfallData.Length指的是2d数组中的项目数-在您的情况下为2 x 5 = 10 ,但是您将该索引放在第一维中并导致超出范围的错误。

If I were you I would have started my code like this: 如果我是你,我会像这样开始我的代码:

int columns = 2;
int days = 5;

string [,] rainfallData = new string[columns, days];

Now each and every time that refer to the bounds of each dimension use columns and days respectively. 现在,每次引用每个维度范围的days分别使用columnsdays

The you'd write your code this way: 您将以这种方式编写代码:

//Ask the user to enter five days of the week and rainfall data for each day
Console.WriteLine("Please enter 5 days of the week.");

//Store the data in a two dimensional string array named rainfallData[]

for (int j = 0; j < days; j++)
{
    rainfallData[0, j] = Console.ReadLine();
}
Console.WriteLine("Please enter the corresponding rain data.");
for (int j = 0; j < days; j++)
{
    rainfallData[1, j] = Console.ReadLine();
}

Console.WriteLine("Data placed in raindallData[] array.");

for (int i = 0; i < columns; i++)
{
    Console.WriteLine();
    for (int j = 0; j < days; j++)
    {
        Console.WriteLine("rainfallData({0},{1})={2}", i, j, rainfallData[i, j]);
    }
}

//Use iteration to calculate the following from the values in rainfallData[]:
//a) sum
double rainsum = 0.0;
Console.Write("Data values calculated using iteration. \n a) Sum of rainfallData[] = ");
for (int i = 0; i < columns; i++)
{
    Console.WriteLine();
    for (int j = 0; j < days; j++)
    {
        rainsum += double.Parse(rainfallData[i, j]);
    }
}
Console.WriteLine(rainsum);

Just for your edification here's how I might have really tackled this: 仅出于您的兴趣,这就是我可能真正解决过的问题:

int days = 5;

Console.WriteLine("Please enter 5 days of the week.");
string[] first = Enumerable.Range(0, days).Select(x => Console.ReadLine()).ToArray();

Console.WriteLine("Please enter the corresponding rain data.");
string[] second = Enumerable.Range(0, days).Select(x => Console.ReadLine()).ToArray();

string[][] rainfallData = new[] { first, second };

Console.WriteLine("Data placed in raindallData[][] jaggard array.");
Console.WriteLine(String.Join("", Enumerable.Range(0, days).Select(x => $"rainfallData[0][{1}]={rainfallData[0][x]}\n")));

Console.WriteLine(String.Join(Environment.NewLine, Enumerable.Range(0, days).Select(x => $"rainfallData[1][{1}]={rainfallData[1][x]}")));

double rainsum = rainfallData.Sum(i => i.Sum(j => double.Parse(j)));
Console.WriteLine($"Data values calculated using iteration. \n a) Sum of rainfallData[][] = {rainsum}");

Here's an even more robust way: 这是一种更可靠的方法:

double[] readNumbers(int items, string title)
{
    Console.WriteLine($"Please enter {items} of {title} data.");
    int attempt = 0;
    return
        Enumerable
            .Range(0, items)
            .Select(x =>
            {
                double result;
                while (!double.TryParse(Console.ReadLine(), out result))
                {
                    Console.WriteLine($"Input not a number. Please try again.");
                }
                return result;
            })
            .ToArray();
}

int days = 5;

double[] first = readNumbers(days, "first columnn");
double[] second = readNumbers(days, "rain data");

double[][] rainfallData = new[] { first, second };

Console.WriteLine("Data placed in raindallData[][] jaggard array.");
Console.WriteLine(String.Join("", Enumerable.Range(0, days).Select(x => $"rainfallData[0][{1}]={rainfallData[0][x]}\n")));

Console.WriteLine(String.Join(Environment.NewLine, Enumerable.Range(0, days).Select(x => $"rainfallData[1][{1}]={rainfallData[1][x]}")));

double rainsum = rainfallData.Sum(i => i.Sum());
Console.WriteLine($"Data values calculated using iteration. \n a) Sum of rainfallData[][] = {rainsum}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM