Java Reference
In-Depth Information
Display 9.1
Handling a Special Case without Exception Handling
(part 1 of 2)
1
import
java.util.Scanner;
2
public
class
DanceLesson
3{
4
public
static
void
main(String[] args)
5
{
6
Scanner keyboard =
new
Scanner(System.in);
7
8
System.out.println("Enter number of male dancers:");
9
int
men = keyboard.nextInt();
10
System.out.println("Enter number of female dancers:");
11
int
women = keyboard.nextInt();
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
}