Java >> Java tutorial >  >> Java

BMI-beregner i Java

Hvad er BMI?

BMI eller Body Mass Index er kropsmassemåling af en person baseret på deres højde og vægt. Ved at bruge BMI kan en person klassificeres i undervægtig, normal, overvægtig eller fede osv. i henhold til tabellen nedenfor.

BMI-formel

BMI = kg/m2 

hvor kg er vægten af ​​individ og m2 er kvadrathøjden af ​​individ

Formel for BMI i Lbs

BMI = lbs/in2 

hvor lbs er vægten af ​​individet og in2 er den kvadratiske højde af individet

Her i dette selvstudie lærer du, hvordan du skriver:

1) Simpelt Java-program til at beregne BMI for en person

2) Avanceret Java-program til at beregne BMI for en person og udskrive, hvis personen er undervægtig, normal, overvægtig eller fede i henhold til følgende BMI-kategorier

  • Mindre end 15 =Meget alvorlig undervægt
  • bmi>=15 men bmi <16 =Svært undervægtig
  • bmi>=16 men bmi <18,5 =Undervægtig
  • bmi>=18,5 men bmi <25 =Normal (sund vægt)
  • bmi>=25, men bmi <30 =Overvægtig
  • bmi>=30 men bmi <35 =Moderat overvægtig
  • bmi>=35 men bmi <40 =Svært overvægtig
  • bmi> 40 =Meget svært overvægtig

1) Java-program til beregning af BMI

//Java program to calculate the BMI

//Importing the Scanner package
import java.util.Scanner;

//Main / Drived Class
public class Main
{
    //Main Function
    public static void main(String[] args) {
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Giving the hint to user what has to enter
        System.out.println("Enter your weight unit (kg or lbs): ");
        //Taking Weight Unit from user
        String unit = input.nextLine();
        //Giving the hint to user what has to enter
        System.out.println("Enter your Weight : ");
        //Taking weight from user
        double weight = input.nextDouble();

        double height = 0;
        //Checking which weight unit has been selected by user
        if(unit.equals("kg")){
            //If unit is kg
            //Giving the hint to user what has to enter
            System.out.println("Enter your Height(In Meters) : ");
            //Taking height(in Meters) from user
            height = input.nextDouble();
        }else if(unit.equals("lbs")){
            //Giving the hint to user what has to enter
            System.out.println("Enter your Height(In Inches) : ");
            //Taking height(in Inches) from user
            height = input.nextDouble();
        }
        //Declaring a double type parameter
        double bmi;
        /*Switch Cases on weight units because BMI Formula changes according to
        /* the Weight Units
        */
        switch (unit){
            //If unit is kg
            case "kg":
                //caluclate the bmi with formula and store it in variable
                bmi = weight / (height * height);
                //print the BMI
                System.out.println("YOUR BMI IS : "+ bmi +" kg/m2");
                break;
            //If unit is lbs
            case "lbs":
                //caluclate the bmi with formula and store it in variable
                bmi = 703 * (weight / (height * height));
                //print the BMI
                System.out.println("YOUR BMI IS : "+ bmi +" lbs/in2");
                break;
            default:
                System.out.println("Please choose correct weight unit");
        }
    }
} 

Output

Enter your weight unit (kg or lbs): 
lbs
Enter your Weight : 
170
Enter your Height(In Inches) : 
69
YOUR BMI IS : 25.101869355177485 lbs/in2

2) Java-program til at beregne BMI og sundhedsstatus

//Java program to calculate the BMI

//Importing the Scanner package
import java.util.Scanner;

//Main / Drived Class
public class Main
{
    //Main Function
    public static void main(String[] args) {
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Giving the hint to user what has to enter
        System.out.println("Enter your weight unit (kg or lbs): ");
        //Taking Weight Unit from user
        String unit = input.nextLine();

        //Giving the hint to user what has to enter
        System.out.println("Enter your Weight : ");
        //Taking weight from user
        double weight = input.nextDouble();

        double height = 0;
        //Checking which weight unit has been selected by user
        if(unit.equals("kg")){
            //If unit is kg
            //Giving the hint to user what has to enter
            System.out.println("Enter your Height(In Meters) : ");
            //Taking height(in Meters) from user
            height = input.nextDouble();

        }else if(unit.equals("lbs")){
            //Giving the hint to user what has to enter
            System.out.println("Enter your Height(In Inches) : ");
            //Taking height(in Inches) from user
            height = input.nextDouble();
        }
        //Declaring a double type parameter
        double bmi = 0;
        /*Switch Cases on weight units because BMI Formula changes according to
        /* the Weight Units
        */
        switch (unit){
            //If unit is kg
            case "kg":
                //caluclate the bmi with formula and store it in variable
                bmi = weight / (height * height);
                //print the BMI
                System.out.println("YOUR BMI IS : "+ bmi +" kg/m2");
                break;
            case "lbs":
                //caluclate the bmi with formula and store it in variable
                bmi = 703 * (weight / (height * height));
                //print the BMI
                System.out.println("YOUR BMI IS : "+ bmi +" lbs/in2");
                break;
            default:
                System.out.println("Please choose correct weight unit");
        }

        //Cheking bmi with BMI CATEGORIES
        if(bmi < 15){
            System.out.println("You are in 'Very severely underweight' Category");

        }else if(bmi >= 15 && bmi < 16){
            System.out.println("You are in 'Severely underweight' Category");

        }else if(bmi >= 16 && bmi < 18.5){
            System.out.println("You are in 'Underweight' Category");

        }else if(bmi >= 18.5 && bmi < 25){
            System.out.println("You are in 'Normal (healthy weight)' Category");

        }else if(bmi >= 25 && bmi < 30){
            System.out.println("You are in 'Overweight' Category");

        }else if(bmi >= 30 && bmi < 35){
            System.out.println("You are in 'Moderately obese' Category");

        }else if(bmi >= 35 && bmi < 40){
            System.out.println("You are in 'Severely obese' Category");

        }else if(bmi > 40){
            System.out.println("You are in 'Very severely obese' Category");

        }

    }
} 

Output

Enter your weight unit (kg or lbs): 
kg
Enter your Weight : 
123
Enter your Height(In Meters) : 
1.98
YOUR BMI IS : 31.37434955616774 kg/m2
You are in 'Moderately obese' Category


Java tag