如何返回||声明在java中工作? [英] How does returning an || statement work in java?

查看:84
本文介绍了如何返回||声明在java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,所以我一直在CodingBat.com上练习。在Recursion-2部分,有一个问题我无法解决(递归)所以我查找了一个答案。找到的解决方案包含以下代码...

I'm new to programming so I have been working through the exercises at CodingBat.com. In the Recursion-2 section, there is a problem I wasn't able to solve (recursively) so I looked up an answer. The found solution contained the following code...

public boolean split53(int[] nums) {

return helper(0, nums, 0, 0);
} 
private boolean helper(int start, int[] nums, int sum1, int sum2) {
    if (start >= nums.length) return sum1 == sum2;
    if (nums[start] % 5 == 0)
        return helper(start + 1, nums, sum1 + nums[start], sum2);
    if (nums[start] % 3 == 0)
        return helper(start + 1, nums, sum1, sum2 + nums[start]);

    return helper(start + 1, nums, sum1 + nums[start], sum2)
            || helper(start + 1, nums, sum1, sum2 + nums[start]);
}

返回什么或其他什么意味着什么?! (上一个返回语句)对于要工作的代码必须在2个值之间交替,但我找不到任何提及返回||任何声明。

What the heck does it mean to return something or something else?! (Last return statement) For the code to work is has to be alternating between the 2 values, but I can't find any mention of return an || statement anywhere.

推荐答案

return helper(start + 1, nums, sum1 + nums[start], sum2)
        || helper(start + 1, nums, sum1, sum2 + nums[start]);

返回 true 如果要么致电 helper()返回true。这是相同的代码编写一个不同但功能相同的方式:

returns true if either call to helper() returns true. Here is the same code written a different but functionally equivalent way:

boolean helperResponse1 = helper(start + 1, nums, sum1 + nums[start], sum2);

if (helperResponse1 == true)
    return true;
else {
    boolean helperResponse2 = helper(start + 1, nums, sum1, sum2 + nums[start]);
    if (helperResponse2 == true)
        return true;
    else
        return false;
}

为了清楚起见,我提出了许多不必要的东西。

I put in a lot of unnecessary stuff for the sake of clarity.

这篇关于如何返回||声明在java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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