我需要编写一个循环,每百万次迭代打印一次消息。我想让它运行10秒(时钟时间)以查看打印了多少条语句。每100万次迭代打印一次消息
我想我只是绑住自己在海里,现在虽然...
public class OneInAMillion{
public static void main(String []args){
long startTime = System.currentTimeMillis(); //time at start of execution
long endTime = startTime + 10000; //time at end of execution (10000 = 10 seconds)
int count = 1;
while(System.currentTimeMillis() < endTime) { //run this before 10 seconds is up
for (int i = 0; i < count; i++) {
if(i % 1000000 == 0) {
System.out.println("Iteration: " + count++); //print every millionth iteration
}
}
}
System.out.println("Time up!"); //print once loop finishes
}
}
什么问题?有没有错误,或者它不工作?你有什么尝试? – CodeChimp 2013-04-23 10:57:31
您只能在while循环中使用,并在里面增加i。 – BobTheBuilder 2013-04-23 10:58:19