创建行和列以列出数组

时间:2016-02-11 02:33:28

标签: c# arrays for-loop console-application

我有一个包含30行和5列的csv文件。我使用流阅读器读入了我的应用程序,然后放入一个数组,排序,现在写入文件。我有5列显示“得分”,后面有破折号。我的问题是我需要在排序后的5列中输出我的数组。我有一个for循环遍历数组的长度。我真的需要它迭代30行和5列,但我不知道该怎么做。我道歉,但我是C#的新手和linq等功能。这是我的输出和for循环的简短版本。如果需要进一步澄清,请告诉我。

int n;

  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (n = 1; n <= numOfScores; n++)
  fileOut.WriteLine("{0,4:f}", scoreArray[n]);
  fileOut.WriteLine();

我知道必须有一个简单的方法来做到这一点,只是不确定。

当前输出如下:

Score    Score    Score    Score    Score
-----    -----    -----    -----    -----
97.05
96.52
93.16
92.44
91.05
90.66
90.59
//etc, etc, through entire array, only one line when it needs to be in each column.

谢谢, 乔

3 个答案:

答案 0 :(得分:1)

为了做到这一点,我首先要处理你的数据。你有一个排序的分数数组。您希望将此输出到列,其中数据在填充之前填充 down ,通过输出流(控制台)强烈偏向于在之前写入下。

这意味着为了避免输出流中的反向跟踪(缓慢而棘手),我们希望在将数据写入控制台之前按下内存中的数据:

double[] scores = ...; //sorted data in here

// now some constants
const int cols = 5;
const int colWidth = 5;
const int colSpace = 4;
const string Header = "Score";

//figure out how many rows we need
int remainder = scores.Length % cols;
int rows = scores.Length / cols + (remainder > 0?1:0);

//organize the data into a 2d structure that matches the output
// I chose an array of arrays rather than 2d array so I can pass individual
//   arrays to format function later on.
var data = new string[rows][];
int i = 0; //score index
for (int c = 0;c < cols;c++)
{
    for (int r = 0;r < rows && i < scores.Length; r++)
    {
        //make sure nested array exists and is pre-populated with empty strings (string.Format() will choke later if we leave these as nulls)
        data[r] = data[r] ?? Enumerable.Repeat("", cols).ToArray();

        //skip this cell if it's at the bottom row of a later column in an unbalanced array
        if (remainder > 0 && r == rows - 1 && c >= remainder) continue;

        data[r][c] = scores[i].ToString();
        i++;
    }
}

//write the header
var format = string.Join("", Enumerable.Repeat("{0,-" + (colWidth + colSpace) + "}", cols));
Console.WriteLine(format, Header);
Console.WriteLine(format, new string('-', colWidth));

//write the data
format = string.Join("", Enumerable.Range(0,cols).Select(i => string.Format("{{{0},-{1}}}{2}", i, colWidth, new string(' ',colSpace))).ToArray());
for (int i = 0; i < rows; i++)
    Console.WriteLine(format, data[i]);

使用此示例数据运行代码:

double[] scores = { 97.05,96.52,93.16,92.44,91.05,90.66,90.59,19.1, 18.4, 16.8, 11.1, 13.8, 12.2, 7.9, 8.1, 11.0, 14.5, 16.6, 21.3, 16, 17.9};
scores = scores.OrderBy(s => s*-1).ToArray();

我得到了这个结果:

Score    Score    Score    Score    Score    
-----    -----    -----    -----    -----    
97.05    90.66    18.4     16       11.1     
96.52    90.59    17.9     14.5     11       
93.16    21.3     16.8     13.8     8.1      
92.44    19.1     16.6     12.2     7.9      
91.05                                                   

这里很酷的是,这段代码可以让您轻松调整所需的列数。

答案 1 :(得分:0)

只需使用Console.Write,而不是使用Console.WriteLine。每次调用WriteLine时,输入后都会打印一个换行符。

  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (n = 1; n <= numOfScores; n++)
  fileOut.Write("{0,4:f}", scoreArray[n]);
  fileOut.WriteLine(); //do this so there is a new line after all the data

答案 2 :(得分:0)

你的数组索引从1开始有点奇怪。它应该从0开始(除非你在第一个索引上跳过某些东西?)。

这可能就是你要找的东西。确保索引不受限制(您的数组可以被5整除)。

  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (int n = 0; n < numOfScores; n+=5) // why do you need to declare n outside?
  {
            fileOut.WriteLine("{0,4:f} {1,4:f} {2,4:f} {3,4:f} {4,4:f}{5}", scoreArray[n],scoreArray[n+1],scoreArray[n+2],scoreArray[n+3],scoreArray[n+4],Environment.NewLine);
  }