假设我有以下代码。在调试时,我希望Eclipse在完成100万次迭代后停止。如何实现?我无法手动执行100万次。
for(int i = 0; i < 10000000; i++) {
//some code
}
在Eclipse中可以设置条件断点:
放置条件代码
i == 1000000
while(fileScanner.hasNext())
从文件扫描器读取行时?如何在读取了一定数量的行后停止? - Floellafor(int i = 0; i < 10000000; i++)
{
if(i==1000000){
// do nothing ....
// this is just a dummy place to make eclipse stop after million iterations
// just put a break point here and run the code until it reaches this breakpoint
// remove this if condition from your code after you have debugged your problem
}
//your code
}
我知道我在回答@Anarelle的问题时可能有些迟了,但我要回答关于目前被接受的答案中没有变量可用时如何停止的问题。这也回答了原始问题。在Eclipse的条件断点窗口(在调试透视图中),您可以单击命中次数:旁边的复选框,然后简单地给出断点在执行之前应该“触发”多少次。请注意,这不仅适用于循环外(例如,在尝试此操作3次后暂停调试),而且还考虑到外部循环。例如,在以下代码中,如果我的命中次数为20,则在达到断点时i
将为6,j
将为3:
for (int i = 0; i < 100; i++) {
System.out.println(i);
for (int j = 2; j < 5; j++) {
System.out.println(j);
}
}
一旦断点被触发,它将被禁用,直到用户重新启用。换句话说,如果每次重新启用,这个功能也可以用来检查特定的断点是否被触发了20次。