What's wrong with this code?

The following code segment should display "AM" in ampmJLabel if the hour is a value in the range 0–11 and should display "PM" in ampmJLabel if the hour is a value in the range 12–23. For any other hour value, the code segment should display "Time Error" in ampm- JLabel. Find the error(s) in the following code:

```
1 int hour = 14;
2
3 if ( hour >= 0 )
4 {
5 if ( hour < 12 )
6 {
7 ampmJLabel.setText( "AM" );
8 }
9 }
10 else
11 {
12 ampmJLabel.setText( "Time Error." );
13 }
14 else if ( hour >= 12 )
15 {
16 if ( hour < 24 )
17 {
18 ampmJLabel.setText( "PM" );
19 }
20 }
```


In the original code, the else clause on lines 10–13 must be moved after line 20 oth- erwise a syntax error occurs at line 14. The reason for this syntax error at line 14 is that the else at line 14 does not have a matching if. Also, the nested if statement at lines 14–20 of the original code should appear before the if statement at lines 3–9; otherwise, the statement starting at line 3 will execute for all values greater than or equal to 0.


```
1 int hour = 14;
2
3 if ( hour >= 12 )
4 {
5 if ( hour < 24 )
6 {
7 ampmJLabel.setText( "PM" );
8 }
9 }
10 else if ( hour >= 0 )
11 {
12 if ( hour < 12 )
13 {
14 ampmJLabel.setText( "AM" );
15 }
16 }
17 else
18 {
19 ampmJLabel.setText( "Time Error." );
20 }
```

Computer Science & Information Technology

You might also like to view...

Describe the main advantages of using the prototyping approach when building a database system.

What will be an ideal response?

Computer Science & Information Technology

In the accompanying figure, Item 1 tells you whether the ____.

A. printer is out of paper B. correct printer is selected C. printer is offline D. correct document is selected

Computer Science & Information Technology

The caption that displays in a label cannot be changed in Design view when creating a form.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Which of the following are so called short-circuit operators?

a. && b. & c. || d. |

Computer Science & Information Technology