打破java中的递归 [英] Breaking out of a recursion in java

查看:100
本文介绍了打破java中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

递归是一种分而治之的风格,它在变小时分裂(树数据结构),如果发现违规,我希望它完全破坏,意味着打破所有递归路径,并返回真正。这可能吗?

The recursion is sort of a 'divide and conquer' style, it splits up while getting smaller (Tree data structure), and I want it to break completely if a violation is found, meaning break all the recursive paths, and return true. Is this possible?

推荐答案

您可以返回错误代码,或修改一些全局变量,以便每个递归实例都知道杀死本身。

You could return an error code, or modify some global variable so that each recursive instance knows to "kill itself".

有点类似。

int foo(bar){
     int to_the_next;

      if (go_recursive){
            to_the_next = foo(whisky_bar);

            if (to_the_next ==DIE) return DIE;
      }

      if (something_unexpected_happened) return DIE;

      process;//may include some other recursive calls, etc etc
}

这篇关于打破java中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆