[英]How can we find out the number of times the while loop lasted
while((MAP = inputFile.readLine()) != null) {
System.out.println(MAP);
}
How can we find out the number of iterations performed by the while loop? 我们如何找出while循环执行的迭代次数? In this textfile, they can be sometimes 5 lines of data, or 100 lines of data..If they are 5 lines, the while loop probably performed 6 loops. 在此文本文件中,有时它们可能是5行数据或100行数据。如果它们是5行,则while循环可能执行了6次循环。 I want that number. 我想要那个号码。
Any suggestions? 有什么建议么?
int count = 0;
while((MAP = inputFile.readLine()) != null) {
System.out.println(MAP);
count++;
}
System.out.println(count);
将while循环外部的变量设置为计数器,然后在while循环中递增计数器。
int iterations = 0;
while((MAP = inputFile.readLine()) != null) {
System.out.println(MAP);
iterations++;
}
int i = 0;
while((MAP = inputFile.readLine()) != null) {
i++;
System.out.println(MAP);
// Some other stuff
}
System.out.println(i);
Set a variable to 0 before the while loop and increment the variable inside the while loop. 在while循环之前将变量设置为0,并在while循环内将变量递增。 Output the variable after the while loop. 在while循环之后输出变量。
如果可以使用java.io.LineNumberReader.getLineNumber()
为什么还要自己做呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.