Java 跳出递归循环问题解决办法
使用异常跳出循环
1、如果方法体内含有需要抛出异常的对象,让方法直接抛出异常,不要在方法体内捕获
publicvoidxxxx()throwsException
2、如果方法体内不含有需要抛出异常的对象
classTest{
staticclassStopMsgExceptionextendsRuntimeException{
}
publicstaticvoidmain(Stringargs[]){
try{
run(0);
}catch(StopMsgExceptione){
System.out.println(e);
}
}
publicstaticvoidrun(intt){
if(t>20){
//跳出
thrownewStopMsgException();
}
//执行操作
System.out.println(t);
//递归
run(t+1);
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短