I cant seem to get the example output, my code returns true, true, false and false. Is my ranging not missing any additional conditional statement?

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

I cant seem to get the example output, my code returns true, true, false and false. Is my ranging not missing any additional conditional statement?

问题

这是您提供的代码:

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (hourOfDay < 0 || hourOfDay >= 23) {
        return false;
    }
    if (hourOfDay < 8 || hourOfDay >= 22) {
        return true;
    } else {
        return false;
    }
}

如果需要修改的话,可以考虑将条件简化为以下方式:

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (hourOfDay < 0 || hourOfDay >= 23) {
        return false;
    }
    return barking && (hourOfDay < 8 || hourOfDay >= 22);
}
英文:

I've the following scenario:
We have a dog that likes to bark. We need to wake up if the dog is barking at night!

Write a method shouldWakeUp that has 2 parameters.

1st parameter should be of type boolean and be named barking it represents if our dog is currently barking.
2nd parameter represents the hour of the day and is of type int with the name hourOfDay and has a valid range of 0-23.

We have to wake up if the dog is barking before 8 or after 22 hours so in that case return true.

In all other cases return false.

If the hourOfDay parameter is less than 0 or greater than 23 return false.

Examples of input/output:

  • shouldWakeUp (true, 1) → should return true
  • shouldWakeUp (false, 2) → should return false since the dog is not barking.
  • shouldWakeUp (true, 8) → should return false, since it's not before 8.
  • shouldWakeUp (true, -1) → should return false since the hourOfDay parameter needs to be in a range 0-23.

and this is my code:

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (hourOfDay &lt; 0 || hourOfDay &gt;= 23) {
        return false;
    }
    if (hourOfDay &lt; 8 || hourOfDay &gt;= 22) {
        return true;
    } else {
        return false;
    }
}

答案1

得分: 2

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (!barking)
        return false;
    if (hourOfDay &lt; 0 || hourOfDay &gt; 23)
        return false;
    return hourOfDay &lt; 8 || hourOfDay &gt;= 22;
}

**输出:**

System.out.println(shouldWakeUp(true, 1));      // true
System.out.println(shouldWakeUp(false, 2));     // false
System.out.println(shouldWakeUp(true, 8));      // false
System.out.println(shouldWakeUp(true, -11));    // false
英文:

You forgot to use boolean barking and hourOfDay could be 23:

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (!barking)
        return false;
    if (hourOfDay &lt; 0 || hourOfDay &gt; 23)
        return false;
    return hourOfDay &lt; 8 || hourOfDay &gt;= 22;
}

Output:

System.out.println(shouldWakeUp(true, 1));      // true
System.out.println(shouldWakeUp(false, 2));     // false
System.out.println(shouldWakeUp(true, 8));      // false
System.out.println(shouldWakeUp(true, -11));    // false

答案2

得分: 0

public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if (hourOfDay < 0 || hourOfDay > 23) {
        return false;

    } else if (barking && (hourOfDay < 8 || hourOfDay > 22)) {
        return true;

    } else {
        return false;
    }
}
英文:
  public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
        if (hourOfDay &lt; 0 || hourOfDay &gt; 23) {
            return false;

        } else if (barking &amp;&amp; (hourOfDay &lt; 8 || hourOfDay &gt; 22)) {
            return true;

        } else {
            return false;
        }
    }

答案3

得分: 0

public class Dog {
    public static void main(String[] args) {
        boolean a = shouldWakeUp(false, 2); // 适用于所有值的正常工作
        System.out.println(a);
    }

    public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
        if ((barking) && ((hourOfDay > 22) || (hourOfDay < 8)) && (hourOfDay >= 0) && (hourOfDay <= 23)) {
            return true;
        }
        return false;
    }
}
英文:
public class Dog {
	public static void main(String[] args) {
		boolean a=shouldWakeUp(false,2);//works fine for all values
		System.out.println(a);
	}
	public static boolean shouldWakeUp(boolean barking,int hourOfDay) {
		if((barking)&amp;&amp;((hourOfDay&gt;22)||(hourOfDay&lt;8))&amp;&amp;(hourOfDay&gt;=0)&amp;&amp;(hourOfDay&lt;=23)) {
				return true;
	}return false;
	}
}

huangapple
  • 本文由 发表于 2020年10月12日 14:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64312762.html
匿名

发表评论

匿名网友

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

确定