Abhiraj Ghosh
Abhiraj Ghosh

Reputation: 61

Can anyone see where i am wrong , My code should return true but its returning false which it should not be returning

public class BarkingDog {
public static void main(String[] args) {
    shouldWakeUp(true , 23);
}
public static boolean shouldWakeUp(boolean barking, int hourOfDay) {
    if(barking==true) {
        if(hourOfDay>=0 && hourOfDay<=23){
            if(hourOfDay<8 && hourOfDay>22){
                System.out.println("WakeUp");
                return true;

            }
        }
    }
    System.out.println("No need to WakeUp");
    return false;

}

This code should return true and should print "Wakeup", but its returning false and printing "No need to sleep". where is the logical error here i want to know.

Upvotes: 1

Views: 28

Answers (2)

reinmmar
reinmmar

Reputation: 1

Take a look at the last condition, it should be hourOfDay<8 || hourOfDay>22. These two lines mean a logical or instead of a and. Right now 23 can't be smaller than 8 as well as larger than 22.

Upvotes: 0

Marcel Preda
Marcel Preda

Reputation: 1205

hourOfDay<8 && hourOfDay>22

It is always false. So, the if branch will never be executed.

Upvotes: 1

Related Questions