In this activity, you will trace through an example of a “dangling else”. The code is supposed to determine whether a time (such as 9 am) is in the morning, afternoon, or evening. For each input set, click on the conditions of the if
statement that are tested and then on the statement that is executed (which may be another if
statement). Be sure to trace what actually happens, even though the result will not be correct.
int hour = in.nextInt(); String suffix = in.next(); String time = "evening"; if (suffix.equals("pm")) if (hour < 6) time = "afternoon"; else time = "morning"; System.out.println("Good " + time);
hour | suffix | time | Output |
---|