我得到错误的输出,请帮忙>>>>>

huangapple go评论68阅读模式
英文:

I M getting wrong output pls help>>>>>

问题

当我输入hourOfDay为-1-2-3......返回值应该为false但实际上却为true :( 

package com.company;

public class BarkingDog {
    public static boolean shouldWakeUp(boolean barking, int hourOfDay){
        if(barking && hourOfDay > 24 && hourOfDay < 0){
            return false;
        }
        else if(barking && hourOfDay < 8 && hourOfDay < 23){
            return true;
        }

    return false;
    }
}
英文:

When I put the value of hourOfDay -1, -2, -3..... the returned value should be false but it is coming true 我得到错误的输出,请帮忙>>>>>

package com.company;

public class BarkingDog {
    public static boolean shouldWakeUp(boolean barking, int hourOfDay){
        if(barking &amp;&amp; hourOfDay &gt;24 &amp;&amp; hourOfDay &lt;0){
            return false;
        }
        else if(barking &amp;&amp; hourOfDay &lt; 8 &amp;&amp; hourOfDay &lt; 23){
            return true;
        }

    return false;
    }
}

答案1

得分: 1

看起来你想在那里使用 ||,而不是 &amp;&amp;

if (barking || (hourOfDay > 24 || hourOfDay < 0)) {
    return false;
}

虽然整个方法似乎可以简化为一个表达式:

return barking && hourOfDay > 8 && hourOfDay < 23;
英文:

Looks like you meant to use || there instead of &amp;&amp;:

if (barking &amp;&amp; (hourOfDay &gt;24 || hourOfDay &lt;0)) {
    return false;
}

Although it seems the entire method can be simplified to a single expression:

return barking &amp;&amp; hourOfDay &gt; 8 &amp;&amp; hourOfDay &lt; 23;

答案2

得分: 0

这对你有效。使用 && 同时适用于两个条件,你应该使用 ||

if (barking && (hourOfDay > 30 || hourOfDay < 0)) {
    return false;
} else if (barking && hourOfDay < 8 && hourOfDay < 25) {
    return true;
}

return false;

}

英文:

This will work for you. Check && is for both conditions and you should use OR ||

if (barking &amp;&amp; (hourOfDay &gt; 30 || hourOfDay &lt; 0)) {
			return false;
		} else if (barking &amp;&amp; hourOfDay &lt; 8 &amp;&amp; hourOfDay &lt; 25) {
			return true;
		}

		return false;
	}

huangapple
  • 本文由 发表于 2020年9月21日 01:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定