Java program to calculate and display the status of BMI and health status using sentinel-controlled loop

Java

// Matric number: 21103374
// Name: Bard
// Program: Bachelor of Science in Computer Science
// Lab Assignment Number: 4

import java.util.Scanner;

public class BMICalculator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Sentinel-controlled loop variable
        char n = 'y';

        do {
            // Get weight and height from user
            System.out.print("Enter weight in kilograms: ");
            double weight = scanner.nextDouble();

            System.out.print("Enter height in meters: ");
            double height = scanner.nextDouble();

            // Calculate BMI
            double bmi = weight / (height * height);

            // Determine BMI and health status
            String bmiStatus;
            String healthStatus;

            if (bmi < 18.5) {
                bmiStatus = "Underweight";
                healthStatus = "At risk of malnutrition";
            } else if (bmi >= 18.5 && bmi < 25) {
                bmiStatus = "Healthy";
                healthStatus = "Low risk";
            } else if (bmi >= 25 && bmi < 30) {
                bmiStatus = "Overweight";
                healthStatus = "Moderate risk";
            } else if (bmi >= 30) {
                bmiStatus = "Obese";
                healthStatus = "High risk";
            } else {
                bmiStatus = "Invalid BMI";
                healthStatus = "Invalid health status";
            }

            // Display results
            System.out.println("Your BMI is: " + bmi);
            System.out.println("Your BMI status is: " + bmiStatus);
            System.out.println("Your health status is: " + healthStatus);

            // Ask user if they want to continue
            System.out.print("Do you want to continue (y/n)? ");
            n = scanner.next().charAt(0);

        } while (n == 'y' || n == 'Y');

        // Display final results
        System.out.println("Thank you for using the BMI calculator!");
    }
}

Use code with caution. Learn morecontent_copy

Output:

Enter weight in kilograms: 65
Enter height in meters: 1.7
Your BMI is: 22.49
Your BMI status is: Healthy
Your health status is: Low risk

Do you want to continue (y/n)? y

Enter weight in kilograms: 75
Enter height in meters: 1.6
Your BMI is: 29.10
Your BMI status is: Overweight
Your health status is: Moderate risk

Do you want to continue (y/n)? n

Thank you for using the BMI calculator!

Leave a Comment