Java 习题 - 代码实现BMI计算


Java 习题 - 代码实现BMI计算_第1张图片
BMI

package com.pro.one;

import java.util.Scanner;
@SuppressWarnings("resource")
public class IndicationBMI {
    public static void main(String[] args) {
        System.out.print("Input your height(m):");
        float height = new Scanner(System.in).nextFloat();
        System.out.println();
        System.out.print("Input your weight(kg):");
        float weight = new Scanner(System.in).nextFloat();
        System.out.println();
        float BMI = weight/(height*height);
        System.out.print("Input your sex(male/female):");
        String sex = new Scanner(System.in).nextLine();
        if ("male".equalsIgnoreCase(sex)) {
            new IndicationBMI().maleBMI(BMI);
            System.out.println("your BMI is "+BMI+".");
        }else if ("female".equalsIgnoreCase(sex)) {
            new IndicationBMI().femaleBMI(BMI);
            System.out.println("your BMI is "+BMI+".");
        }else {
            System.out.println("error!");
        }
        System.out.println("The idea BMI is 22.");
    }
    public void maleBMI(float BMI) {
        if (BMI<20) {
            System.out.println("过轻");
        }else if (BMI<25&&BMI>=20) {
            System.out.println("适中");
        }else if (BMI<30&&BMI>=25) {
            System.out.println("过重");
        }else if (BMI<35&&BMI>=30) {
            System.out.println("肥胖");
        }else {
            System.out.println("非常肥胖");
        }
    }
    private void femaleBMI(float BMI) {
        if (BMI<19) {
            System.out.println("过轻");
        }else if (BMI<24&&BMI>=19) {
            System.out.println("适中");
        }else if (BMI<29&&BMI>=24) {
            System.out.println("过重");
        }else if (BMI<34&&BMI>=29) {
            System.out.println("肥胖");
        }else if (BMI>=34) {
            System.out.println("非常肥胖");
        }
    }
}
//Result:
Input your height(m):1.68

Input your weight(kg):54

Input your sex(male/female):male
过轻
your BMI is 19.132654.
The idea BMI is 22.

你可能感兴趣的:(Java 习题 - 代码实现BMI计算)