Java Reference
In-Depth Information
9
double height1 = console.nextDouble();
10
System.out.print("weight (in pounds)? ");
11
double weight1 = console.nextDouble();
12
double bmi1 = weight1 / (height1 * height1) * 703;
13
System.out.println();
14
15
System.out.printf("Person #1 body mass index = %5.2f\n", bmi1);
16
if (bmi1 < 18.5) {
17
System.out.println("underweight");
18
} else if (bmi1 < 25) {
19
System.out.println("normal");
20
} else if (bmi1 < 30) {
21
System.out.println("overweight");
22
} else { // bmi1 >= 30
23
System.out.println("obese");
24
}
25
}
26 }
Here is a sample execution of the program:
Enter next person's information:
height (in inches)? 73.5
weight (in pounds)? 230
Person #1 body mass index = 29.93
overweight
Two-Person Unstructured Solution
Now that we have a program that computes one person's BMI and weight status,
let's expand it to handle two different people. Experienced programmers would
probably begin by adding structure to the program before trying to make it handle
two sets of data, but novice programmers will find it easier to consider the unstruc-
tured solution first.
To make this program handle two people, we can copy and paste a lot of the code
and make slight modifications. For example, instead of using variables called
height1 , weight1 , and bmi1 , for the second person we will use variables height2 ,
weight2 , and bmi2 .
We also have to be careful to do each step in the right order. Looking at the sample
execution, you'll see that the program prompts for data for both individuals first and
then reports results for both. Thus, we can't copy the entire program and simply paste
a second copy; we have to rearrange the order of the statements so that all of the
prompting happens first and all of the reporting happens later.
 
Search WWH ::




Custom Search