my java bmi calculator giving the same answer

Viewed 65

i'm trying to make a bmi calculator but the answer keeps being the same.

i tried putting numbers height=1.50 meter weight=100 kg but the answer was underweigth

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double bmi = 0;
    double weightinkg = 0;
    double heightinmeter = 0;
    String status = "";
    System.out.println("Enter the weight in Kg :");
    weightinkg = sc.nextDouble();
    System.out.println("Enter the height in Meter : ");  //enter the age
    heightinmeter = sc.nextDouble();
    if ((weightinkg <= 0) && (heightinmeter <= 0))
        System.out.println("ERROR: Negative number");
    double square = Math.pow(heightinmeter, 2);
    bmi = weightinkg / heightinmeter;
    if (bmi >= 0) {
        if (bmi >= 0 && bmi <= 24) {
            status = "Under Weight";
        } else if (bmi >= 25 && bmi <= 29) {
            status = "Healthy";
        } else if (bmi >= 30 && bmi <= 34) {
            status = "Overweight";
        } else
            status = "Obese";
    }
    else
        System.out.println("Invalid BMI (number must be positive");
    System.out.printf("Hello, your status is %s ! Please come again!", status);
}
2 Answers

You didn't use the square. Should be

    double square = Math.pow(heightinmeter, 2);
    bmi = weightinkg / square;

You are not using square.

bmi should be weightinkg/square

Also use the OR operator instead of AND. You can remove the negative check in the end.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double bmi = 0;
        double weightinkg = 0;
        double heightinmeter = 0;
        String status = "";
        System.out.println("Enter the weight in Kg :");
        weightinkg = sc.nextDouble();
        System.out.println("Enter the height in Meter : ");  //enter the age
        heightinmeter = sc.nextDouble();
        if ((weightinkg <= 0) || (heightinmeter <= 0)) { //Replace && with ||
            System.out.println("ERROR: Negative number");
            return;
        }
        double square = Math.pow(heightinmeter, 2);
        bmi = weightinkg / square;
        if (bmi >= 0 && bmi <= 24) {
            status = "Under Weight";
        } else if (bmi >= 25 && bmi <= 29) {
            status = "Healthy";
        } else if (bmi >= 30 && bmi <= 34) {
            status = "Overweight";
        } else
            status = "Obese";

        System.out.printf("Hello, your status is %s ! Please come again!", status);
    }
}
Related