如何在 C# 中读取三个文本文件并将其排序为三行锯齿状数组?

How do I read three text files and sort into three row jagged array in C#?

我的 C# 项目是 Windows 窗体。

我的 bin/debug 文件夹中有三个文本文件,其中包含单独 class 部分的考试成绩。

这是我真正需要帮助的部分:我需要将文件存储在按部分划分的三行锯齿状数组中。


最后,使用交错数组,我需要在文本框中显示每个计算结果:

每个部分的平均分

所有部分的平均分

所有部分的最高分

所有部分的最低分数。


Section1.txt:

 87
 93
 72
 98
 65
 70
 89
 78
 77
 66
 92
 72

Section2.txt:

 71
 98
 93
 79
 84
 90
 88
 91

Section3.txt:

 88
 81
 56
 72
 69
 74
 80
 66
 71
 73

首先,您在 bid/debug 文件夹中存储了三个文本文件。

Section1.txt, Section2.txt, Section3.txt

你能得到这个文本文件path.right? 所以你可以将这个文本文件路径存储到字符串数组中。

其次:你可以通过循环这个字符串从每个文本文件中获取数据array.Here如何从文本文件中读取数据=>

将字符串数据用"Space"或"\n"分割得到Section 1字符串数组。

让我们从将一个文件读入数组开始;在 Linq:

的帮助下很容易
using System.IO;
using System.Linq;

...

string path = @"C:\MyFile1.txt";

int[] result = File
  .ReadLines(path)
  .Select(line => int.Parse(line))
  .ToArray(); 

现在,我们要的不是单个文件,而是其中的 集合

string[] filePaths = new string[] {
  @"C:\MyFile1.txt",
  @"C:\MyFile2.txt",
  @"C:\MyFile3.txt",
};

int[][] result = filePaths
  .Select(path => File //The inner code looks familiar, right?
    .ReadLines(path)
    .Select(line => int.Parse(line))
    .ToArray()) 
  .ToArray();