0

I am having an issue with some java code I have I created, I am attempting to create an if statement contained within a while loop in order for it to run indefinitely while rotating between different print commands based on a variable that is increased each time it goes through the loop. The code should be setting the time variable to 0 and then entering the while loop. The first thing it should always do within the while loop is increase the time variable by one with ++ and then entering the if statement and printing one of the three different possible print commands, and then when the time variable is larger then 24 setting time to 0 and therefore looping back to the first possible print command. I am still learning java and am absolutely terrible at it so my apologies if this question is dumb.

the code:

class Main {
  public static void main(String[] args) {
    int time = 0;
    while (true) {
      time++;
      if (time > 5) {
        System.out.println("Good morning.");
      } else if (time > 12) {
        System.out.println("Good afternoon.");
      } else if (time > 19) {
        System.out.println("Good night.");
      } else if (time > 24) {
        time = 0;
      } else {
        System.out.println("If this message is printed, the code is not working properly.");
      }
    }
  }
}
2
  • 2
    Question to get you on the right track: what value is > 12, but not > 5? --- A comment on the wording: there is not if-loop, it is a statement. Commented Sep 30, 2022 at 19:00
  • In the future, you may want to consider putting a breakpoint in your method and stepping through the code with your IDE's debugger. It'll help you understand how the code is being evaluated. Commented Sep 30, 2022 at 19:10

2 Answers 2

1

Your if statements to not cover the case when time is between 0 and 5. As such when "time" starts at these values your else statement with the error message will be hit

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is never able to reach the 12, 19, and 24 conditions. If the time is say 13, the if statement will first check if the time is greater than 5, which 13 is. So it will enter the first block and print "Good Morning".

To fix this you could change the order that you are checking the time, so that the largest checks come first and if not will fall through to the smaller checks. Try something like this:

class Main {
  public static void main(String[] args) {
    int time = 0;
    while (true) {
      time++;
      if (time > 24) {
        time = 0;
      }else if (time > 19) {
        System.out.println("Good night.");
      } else if (time > 12) {
        System.out.println("Good afternoon.");
      } else if (time > 5) {
        System.out.println("Good morning.");
      } else {
        System.out.println("If this message is printed, the code is not working properly.");
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.