Java Reference
In-Depth Information
We've also decided that when we move to this second stage, we will add code for
the introduction. This code should appear at the beginning of the program and should
include an empty
println
to produce a blank line to separate the introduction from
the rest of the user interaction.
We now combine these elements into a complete program:
1 // This program finds the body mass index (BMI) for two
2 // individuals.
3
4
import
java.util.*;
5
6
public class
BMI2 {
7
public static void
main(String[] args) {
8
System.out.println("This program reads data for two");
9
System.out.println("people and computes their body");
10
System.out.println("mass index and weight status.");
11
System.out.println();
12
13
Scanner console =
new
Scanner(System.in);
14
15
System.out.println("Enter next person's information:");
16
System.out.print("height (in inches)? ");
17
double
height1 = console.nextDouble();
18
System.out.print("weight (in pounds)? ");
19
double
weight1 = console.nextDouble();
20
double
bmi1 = weight1 / (height1 * height1) * 703;
21
System.out.println();
22
23
System.out.println("Enter next person's information:");
24
System.out.print("height (in inches)? ");
25
double
height2 = console.nextDouble();
26
System.out.print("weight (in pounds)? ");
27
double
weight2 = console.nextDouble();
28
double
bmi2 = weight2 / (height2 * height2) * 703;
29
System.out.println();
30
31
System.out.printf("Person #1 body mass index = %5.2f\n", bmi1);
32
if
(bmi1 < 18.5) {
33
System.out.println("underweight");
34
}
else if
(bmi1 < 25) {
35
System.out.println("normal");
36
}
else if
(bmi1 < 30) {
37
System.out.println("overweight");
38
}
else
{ // bmi1 >= 30
Search WWH ::
Custom Search