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...

Rapport has three behavioral components: mutual attention, positivity, and ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

Virtual private networks use which of the following to allow users to log in to the corporate network?

A. Private network B. Internet C. Intranet D. Extranet

Computer Science & Information Technology