所以我刚开始接触 C#,几乎没有任何知识,所以这对我来说更适合学习而不是实际使用。因此,我真正想知道的是如何让我的代码按照我的方式工作,即使有更简单/更快/更智能的解决方案。
所以我想做的是创建一个字符串数组,并使用一个循环从文本文件的每一行中读取到数组的相应元素中。这就是我在这里尝试做的事情,我很想听听您对此有什么解决方案。
{
class Program
{
static void Main(string[] args)
{
StreamReader ki = new StreamReader("kiserlet.txt");
string[] a = new string[15];
Console.ReadLine();
int y = 0;
int n = 0;
for (int i = 0; i > 15; i++)
{
a[n] = Convert.ToString(ki.ReadLine());
n++;
}
for (int x = 0;x > 15;x++)
{
Console.WriteLine(a[y]);
y++;
}
Console.ReadLine();
ki.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将文件的每一行读入一个数组,然后遍历它。
class Program
{
static void Main(string[] args)
{
// this will read all lines from within the File
// and automatically put them into an array
//
var linesRead = File.ReadLines("kiserlet.txt");
// iterate through each element within the array and
// print it out
//
foreach (var lineRead in linesRead)
{
Console.WriteLine(lineRead);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5525 次 |
| 最近记录: |