私は次のコードがありますが、私はコードが行の非整数値を無視するように助けが必要です。現在、各行ごとの合計を計算しますが、非整数値を満たした時点で停止します。また、どのように私はすべての行の総計を得ることができますか?コードを読み取る1行あたりの合計の合計は機能しますが、整数以外の値を無視する必要があります。私はまた総計を見つける必要があります
私の入力ファイルは、この
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 Check Now –