我有下面的代碼,但我需要幫助,使代碼忽略連續的非整數值。目前它計算每行的總和,但是如果它滿足非整數值時停止。我怎麼能得到所有行的總計?要讀取的代碼總計每行的總和,但需要忽略非整數值,仍然有一些總數。我也需要找到總計
我輸入的文件看起來像這樣
50,22,30,10,50,5,40
25,10,10,46,16,17,90
15, c80x,2
X,2,3,
公共類節目 {
static string currentLine; //variable for holding the current line that has been read from the file
static int[] theList; // array to hold integer values read from the theLine
static int LineNumber = 1; //A count keeping the current line's number
static int theSum; //Variable to hold the sum of the numbers successfully ready from the file
static int total;
public static void Main(string[] args)
{
var fileStream = new FileStream(@"InputData.txt", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
//string line;
while ((currentLine = streamReader.ReadLine()) != null)
{
Add(currentLine); // process the line
}
}
Console.ReadLine();
fileStream.Dispose();//Release the file
}
public static void Add(string numbers)
{
if (numbers.Contains(";")) //Check if line contains semi-colon as the delimiter
{
theList = numbers.Trim().Split(',').Select(int.Parse).ToArray();
//add input elements to array excluding the ; character
}
else if (numbers.Contains(",")) //Check if the line contains comma as the delimiter
{
theList = numbers.Trim().Split(',').Select(int.Parse).ToArray();
// add input elements to array excluding the , character
}
else
{
throw new ArgumentException();
}
theSum = theList.Sum();
Console.WriteLine("The sum of the numbers entered at line : " +
LineNumber.ToString() + " is : " + theSum);
LineNumber++;
}
你的輸入文件是什麼樣的? – px06
@ px06立即檢查 –