|
发表于 2021-8-27 00:27:58
|
显示全部楼层
- package com.Study.fishc;
- import java.util.Scanner;
- import java.util.regex.PatternSyntaxException;
- /**
- * @Description BMI指数
- * ----------------------------
- * 体重过轻 BMI <18. 5
- * 正常范围 18. 5<= BMI < 24
- * 体重过重 24<= BMI < 27
- * 轻度肥胖 27<= BMI < 30
- * 中度肥胖 30<= BMI < 35
- * 重度肥胖 35<=BM I
- * -------------------------------
- * BMI的计算公式是体重(kg) / (身高*身高)
- * @Classname BmiWeight
- * @Date 2021/8/26 23:40
- * @Created by 折腾的小飞
- */
- public class BmiWeight {
- public static void main(String[] args) throws Exception {
- System.out.println("请输入身高(m):");
- Scanner scanner = new Scanner(System.in);
- double height = scanner.nextDouble();
- scanner.nextLine(); // 接收空格
- System.out.println("请输入体重(kg):");
- double weight = scanner.nextDouble();
- System.out.println(getBmi(height, weight));
- }
- /**
- * description: 计算BMI指数
- * @since: 1.0.0
- * @author: 涂鏊飞tu_aofei@163.com
- * @date: 2021/8/27 0:12
- * @Param height:
- * @Param weight:
- * @return: java.lang.String
- */
- private static String getBmi(double height, double weight) throws Exception {
- // 判断是否为0或null
- if (weight == 0 || Double.isNaN(weight) || Double.isNaN(height)) {
- throw new ArithmeticException("被除数不能为0!");
- }
- // 体重为0
- if(height==0){
- throw new Exception("身高异常!");
- }
- double bmi = weight / Math.pow(height, 2);
- String[] msg = {"当前的BMI是:", "身体状态是:"};
- String[] info = {"重度肥胖", "中度肥胖", "轻度肥胖", "体重过重", "正常范围", "体重过轻"};
- if (bmi >= 35) {
- return msg[0] + bmi + "\n" + msg[1] + info[0];
- } else if (bmi >= 30) {
- return msg[0] + bmi + "\n" + msg[1] + info[1];
- } else if (bmi >= 27) {
- return msg[0] + bmi + "\n" + msg[1] + info[2];
- } else if (bmi > 24) {
- return msg[0] + bmi + "\n" + msg[1] + info[3];
- } else if (bmi > 18.5) {
- return msg[0] + bmi + "\n" + msg[1] + info[4];
- } else {
- return msg[0] + bmi + "\n" + msg[1] + info[5];
- }
- }
- }
复制代码
|
|