0% found this document useful (0 votes)
24 views4 pages

BMI Calculator

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

BMI Calculator

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package enhancedbmicalculator;

import java.util.Scanner;

public class EnhancedBMICalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String choice;

do {

// Display Heading

System.out.println("\n===============================");

System.out.println(" Enhanced BMI Calculator");

System.out.println("===============================");

// Input weight and validate

double weight = 0;

boolean validWeight = false;

while (!validWeight) {

try {

System.out.print("\nEnter your weight in kilograms (kg): ");

weight = scanner.nextDouble();

if (weight <= 0) {

System.out.println("Weight must be a positive number. Please try again.");

} else {

validWeight = true;

} catch (Exception e) {

System.out.println("Invalid input. Please enter a valid number for weight.");

scanner.next(); // Consume invalid input

}
}

// Input height and validate (height in centimeters)

double heightInCm = 0;

boolean validHeight = false;

while (!validHeight) {

try {

System.out.print("\nEnter your height in centimeters (cm): ");

heightInCm = scanner.nextDouble();

if (heightInCm <= 0) {

System.out.println("Height must be a positive number. Please try again.");

} else {

validHeight = true;

} catch (Exception e) {

System.out.println("Invalid input. Please enter a valid number for height.");

scanner.next(); // Consume invalid input

// Convert height from centimeters to meters

double heightInM = heightInCm / 100;

// Calculate the BMI

double bmi = calculateBMI(weight, heightInM);

// Output the BMI result with heading

System.out.println("\n===============================");

System.out.println(" BMI Calculation");

System.out.println("===============================");

System.out.printf("Your BMI is: %.2f\n", bmi);


displayBMICategory(bmi);

// Allow the user to calculate again or exit

System.out.print("\nWould you like to calculate BMI again? (yes/no): ");

choice = scanner.next().toLowerCase();

} while (choice.equals("yes")); // Loop to allow the user to repeat the calculation

// Exit message with a heading

System.out.println("\n===============================");

System.out.println(" Thank You for Using the BMI Calculator!");

System.out.println("===============================");

scanner.close();

// Method to calculate BMI

public static double calculateBMI(double weight, double height) {

return weight / (height * height);

// Method to display BMI category based on the calculated BMI

public static void displayBMICategory(double bmi) {

if (bmi < 18.5) {

System.out.println("You are underweight.");

} else if (bmi >= 18.5 && bmi < 24.9) {

System.out.println("You have a normal weight.");

} else if (bmi >= 25 && bmi < 29.9) {

System.out.println("You are overweight.");

} else {

System.out.println("You have obesity.");

}
}

You might also like