Java Reference
In-Depth Information
Display 9.2
Handling a Special Case without Exception Handling
(part 2 of 3)
12
if
(men == 0 && women == 0)
13 {
14 System.out.println("Lesson is canceled. No students.");
15 System.exit(0);
16 }
17
else if
(men == 0)
18 {
19 System.out.println("Lesson is canceled. No men.");
20 System.exit(0);
21 }
22
else if
(women == 0)
23 {
24 System.out.println("Lesson is canceled. No women.");
25 System.exit(0);
26 }
27 //
women
>=
0
&&
men
>=
0
28
if
(women >= men)
29 System.out.println("Each man must dance with " +
30 women/(
double
)men + " women.");
31
else
32 System.out.println("Each woman must dance with " +
33 men/(
double
)women + " men.");
34 System.out.println("Begin the lesson.");
35 }
36 }
Sample Dialogue 1
Enter number of male dancers:
4
Enter number of female dancers:
6
Each man must dance with 1.5 women.
Begin the lesson.
Sample Dialogue 2
Enter number of male dancers:
0
Enter number of female dancers:
0
Lesson is canceled. No students.