문제

I hava a method called test() that throws an exception.

I want to write a loop that executes as long as it throws an exception, and breaks when it no longer throws an exception.

Can any one hint me on the logic that must be use?

For example I tried,

int i=0;
while(test())
{
    i++;
}
System.out.println(i);
도움이 되었습니까?

해결책

int i=0;
while (true) {
    try {
        test();
        break;
    } catch (Exception e) {
        i++; // Loop will continue
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top