Java >> Tutoriel Java >  >> Java

Calculateur IMC en Java

Qu'est-ce que l'IMC ?

L'IMC ou indice de masse corporelle est la mesure de la masse corporelle d'un individu en fonction de sa taille et de son poids. En utilisant l'IMC, une personne peut être classée en insuffisance pondérale, normale, en surpoids ou obèse, etc. selon le tableau ci-dessous.

Formule de l'IMC

BMI = kg/m2 

où kg est le poids de l'individu et m2 est la taille au carré de l'individu

Formule de l'IMC en livres

BMI = lbs/in2 

où lbs est le poids de l'individu et in2 est la taille au carré de l'individu

Dans ce didacticiel, vous apprendrez à écrire :

1) Programme Java simple pour calculer l'IMC d'un individu

2) Programme Java avancé pour calculer l'IMC d'un individu et imprimer si l'individu est en sous-poids, normal, en surpoids ou obèse selon les catégories d'IMC suivantes

  • Moins de 15 =très grave insuffisance pondérale
  • bmi>= 15 mais bmi < 16 =grave insuffisance pondérale
  • bmi>=16 mais bmi <18,5 =poids insuffisant
  • bmi>=18,5 mais bmi <25 =Normal (poids santé)
  • bmi>=25 mais bmi <30 =Surpoids
  • bmi>=30 mais bmi <35 =modérément obèse
  • bmi>=35 mais bmi <40 =sévèrement obèse
  • bmi> 40 =très sévèrement obèse

1) Programme Java pour calculer l'IMC

//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");
        }
    }
} 

Sortie

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) Programme Java pour calculer l'IMC et l'état de santé

//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");

        }

    }
} 

Sortie

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


Balise Java