欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 跳出遞歸循環(huán)問題解決辦法

 更新時間:2017年07月07日 09:34:06   投稿:lqh  
這篇文章主要介紹了 Java 跳出遞歸循環(huán)問題解決辦法的相關資料,需要的朋友可以參考下

 使用異常跳出循環(huán)

1、如果方法體內(nèi)含有需要拋出異常的對象,讓方法直接拋出異常,不要在方法體內(nèi)捕獲

public void xxxx() throws Exception

2、如果方法體內(nèi)不含有需要拋出異常的對象

class Test {
   static class StopMsgException extends RuntimeException {
   }
  public static void main(String args[]) {
    try {
      run(0);
    } catch (StopMsgException e) {
      System.out.println(e);
    }
  }
 
  public static void run(int t) {
 
    if (t > 20) {
      // 跳出
      throw new StopMsgException();
    }
    // 執(zhí)行操作
    System.out.println(t);
    // 遞歸
    run(t + 1);
  }
}

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

最新評論