Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is a java BMI calculator?

Asked by Tamura Takada Oct 18, 2022 1.1K views 1 answer
Share

About this question

 My task:


Body Mass Index (BMI) is a measure of health based on height and weight. It can be calculated by taking your weight in kilograms and dividing it by the square of your height in metres.

Write a java code to let the user enter weight, feet, and inches and interpret the users BMI.

My code:

//import scanner
import java.util.Scanner;
//import Maths class
import java.lang.Math;
public class ComputeBMI {
   public static void main(String[] args) {
        // TODO Auto-generated method stub
  //create scanner
        Scanner input = new Scanner(System.in);
//declare variables
        double weight;
        int feet;
        int inches;
        //prompt user
        System.out.print("Enter weight in pounds: ");
        weight = input.nextFloat();
        System.out.print("Enter feet: ");
        feet = input.nextInt();
        System.out.print("Enter inches: ");
        inches = input.nextInt();
        //convert measurements
        double weightInKilos = weight * 0.453592;
        double heightInMeters = (((feet * 12) + inches) * .0254);
        double bmi = weightInKilos / Math.pow(heightInMeters, 2.0);
//      double bmi = weightInKilos / (heightInMeters * heightInMeters);
        //display output
        System.out.println("Your BMI is: " + bmi);
        //interpret BMI
        if (bmi < 18>            System.out.print("Underweight");
        }
        else if (bmi >= 18.5 && bmi < 25>            System.out.print("Normal");
        }
        else if (bmi >= 25 && bmi < 30>            System.out.print("Overweight");
        }
        else if (bmi >= 30) {
            System.out.print("Obese");
        }
//      Do I need this last else if there?
//      else {
//          System.out.print("");
//      }
        input.close();
    }
}

I used only the material we've been taught thus far to complete my code. My POC is clarity/fluidity of my code and my variable data types. In my mind, feet and inches should be integers and weight should be a double. Valid hypothesis?

Your answer

1 Answer

More Java discussions