【发布时间】:2013-08-16 10:01:28
【问题描述】:
我做了一个简单的程序,我想打印文件中的所有奇数行。
public static void main(String[] args) throws FileNotFoundException{
File file = new File("text.txt");
Scanner fileRead = new Scanner(file);
int lineCount = 0;
int i = 0;
while(fileRead.hasNextLine()){
lineCount++;
i = lineCount % 2;
System.out.println("Line count -- >> " + lineCount);
if(i == 1){
System.out.println(fileRead.nextLine());
}
}
fileRead.close();
}
}
所以当我运行它时,输出是
行数 -- >> 1
奇怪
行数 -- >> 2
行数 -- >> 3
甚至
行数 -- >> 4
行数 -- >> 5
奇怪
等等.... 为什么我让 lineCount 增加了两次?在此先感谢
【问题讨论】: