所以我的代码有一个小问题,我几乎可以肯定它应该是一个简单的解决方案。所以我在一个文件中有一些数据:
25 150
60
63
61
70
72
68
66
68
70
这一点数据的问题在于第一行:" 25 150",假设要保存为整数,以便我可以在整个代码中使用它们。我没有那行数字就解决了问题,因为数组可以像往常那样将它们分开。如何编写代码以便将它们分开并将它们保存为两个不同的整数?这是我到目前为止的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace proj11LEA
{
class Program
{
const int SIZE = 50;
static void Main(string[] args)
{
int[] volts = new int[SIZE];
string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";
Console.WriteLine("\nResistor Batch Test Analysis Program\n");
Console.WriteLine("Data file must be in your Documents folder.");
Console.WriteLine("Please enter the file name: ");
string input = Console.ReadLine();
string path = environment + input;
StreamReader data = new StreamReader(path);
string fromFile;
int count = 0;
int start = 1;
Console.WriteLine("\nRes#\tDissipation\tPassed");
do
{
fromFile = data.ReadLine();
if (fromFile != null)
{
string[] dataArray = fromFile.Split();
volts[count] = int.Parse(dataArray[0]);
count++;
}
} while (fromFile != null);
for (int i = 0; i < count; i++ )
{
int diss = (volts[i] * volts[i])/25;
if (diss >= 150)
{
string answer = ("yes");
Console.WriteLine("{0}\t{1:d2}\t\t{2}", start++, diss , answer);
}
else
{
string answer = ("no");
Console.WriteLine("{0}\t{1:d2}\t\t{2}", start++, diss, answer);
}
}
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
这将从您拥有的数字文本文件中创建一个int列表,然后您可以根据需要进行操作!
List<int> data = new List<int>();
using (StreamReader xReader = new StreamReader("TextFile1.txt"))
{
string line;
while (!xReader.EndOfStream)
{
line = xReader.ReadLine();
string[] input = line.Split(' ', '\r');// this splits the line on the space and newline
foreach (string item in input)
{
data.Add(Convert.ToInt32(item));
}
}
}