if条件语句 保留小数位 及c中的printf方法

package com.sxt.test;

import java.util.Scanner;

public class TestifDemo4 {

// 体质指数(BMI)=体重(kg)÷身高^2(m)
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("请输入您的体重:");
int weight=sc.nextInt();
System.out.println("请输入您的身高(单位米,保留两位小数):");
double height=sc.nextDouble();

double BMI=weight/Math.pow(height, 2);
//math.round()方法保留两位 四舍五入  
double bmi=(double)Math.round(BMI*10)/10;

System.out.println("您的BMI指数为:"+BMI);

// 打印输出保留两位 四舍五入  system.out.printf("%.2f",bmi);

/*过轻:低于18.5
正常:18.5-23.9
过重:24-27
肥胖:28-32
非常肥胖, 高于32*/
if(BMI<18.5){
System.out.println("您的BMI指数过低!");
}else if(BMI>=18.5&&BMI<=24){
System.out.println("您的BMI指数正常");
}else if(BMI>24&&BMI<=27){
System.out.println("您的BMI指数过重");
}else if(BMI>27&&BMI<=32){
System.out.println("您的BMI指数肥胖");
}else {
System.out.println("您的BMI指数非常肥胖!!!!");
}
}
}

原文地址:https://www.cnblogs.com/pcyiren/p/8855974.html