体重指数是以千克为单位的体重除以以米为单位的身高的平方。表示为kg / m ^ 2。
给出了一个计算体重指数(BMI)的程序。
import java.util.Scanner; public class Example { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Input weight in kilogram: "); double weight = sc.nextDouble(); System.out.print("\nInput height in meters: "); double height = sc.nextDouble(); double BMI = weight / (height * height); System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2"); } }
输出结果
Input weight in kilogram: 55 Input height in meters: 1.5 The Body Mass Index (BMI) is 24.444444444444443 kg/m2
现在让我们了解上面的程序。
重量和高度的值分别从用户以千克和米为单位获得。证明这一点的代码片段如下所示-
Scanner sc = new Scanner(System.in); System.out.print("Input weight in kilogram: "); double weight = sc.nextDouble(); System.out.print("\nInput height in meters: "); double height = sc.nextDouble();
然后,使用公式BMI =体重/(身高*身高)来计算体重指数。最后,显示“体重指数”的值。证明这一点的代码片段如下-
double BMI = weight / (height * height); System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2");