Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A simple Body Mass Index (BMI) calculator program that uses
- * if-else statements to determine the BMI status of an individual.
- *
- * @author Tyler Wargo
- * @version 1.0
- */
- import java.util.Scanner;
- public class BMI
- {
- public static void main(String[] args)
- {
- //Declare & Initialize Variables/Objects
- String category;
- Scanner in = new Scanner(System.in);
- //Display BMI Calculator heading
- System.out.println("==========================");
- System.out.println("Body Mass Index Calculator");
- System.out.println("==========================\n");
- //Get inputs for BMI results
- System.out.print("Please enter your first and last name (e.g. John Williams): ");
- String name = in.nextLine();
- System.out.print("Please enter your weight in pounds (e.g. 128): ");
- double lb_weight = in.nextDouble();
- System.out.print("Please enter your height in feet and inches (e.g. 5 11): ");
- String feet = in.next();
- String inches = in.next();
- //Calculate variables for BMI
- double m_height = ((Double.parseDouble(feet) * 12) + Double.parseDouble(inches)) * 0.0254; //Convert feet to inches, parse strings, and convert them to a height in meters using a factor
- double kg_weight = lb_weight * 0.4535; //Convert weight in pounds to weight in kilograms
- double bmi = kg_weight / (m_height * m_height); //Calculate BMI (Also squares the heights)
- //Check if BMI if within a certain range and placing it within a category
- if(bmi >= 29.9)
- {
- category = "Obese";
- }
- else if(bmi >= 25.0)
- {
- category = "Overweight";
- }
- else if(bmi >= 18.5)
- {
- category = "Normal or Healthy Weight";
- }
- else
- {
- category = "Underweight";
- }
- //Display BMI Results heading
- System.out.println("\n=======================");
- System.out.println("Body Mass Index Results");
- System.out.println("=======================\n");
- //Display/Output BMI results and information
- System.out.println("Name: " + name);
- System.out.println("Height (m): " + m_height);
- System.out.println("Weight (kg): " + kg_weight);
- System.out.println("BMI: " + bmi);
- System.out.print("Category: " + category);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement