只比较文件中的最后一列
本文关键字:一列 最后 比较 文件 | 更新日期: 2023-09-27 18:03:41
我有一个文件,如:
outlook temperature Humidity Windy PlayTennis
sunny hot high false N
sunny hot high true N
overcast hot high false P
sunny hot high false P
我基本上想比较第一列和最后一列。想要sunny-n然后occurrence -no ++和sunny-P occurrence _yes++
我把代码写成我已经采取了读取每一行的while循环,我遇到的问题是,每次,它都会进入occur_yes+
+的循环,因为check的值成为最后一列的值,但我想如果最后一列是p,那么它应该进入occur_yes
,否则它应该进入occur_No
。
希望我讲清楚了。
我是一名新生,请帮帮我
这段代码应该满足您的要求:
int occur_yes = 0;
int occur_no = 0;
// open file
using (StreamReader r = new StreamReader("inputFile.txt"))
{
// read lines one at a time
string line;
while ((line = r.ReadLine()) != null)
{
// split the line
string[] cols = line.Split(''t');
if (cols[0] == "sunny")
{
// check last column only if the first one is "sunny"
if (cols[4] == "N")
occur_no++;
else if (cols[4] == "P")
occur_yes++;
}
}
}