-2

Please bear with me, I'm an interactive design student in week 2 of a Java class.

I need to create a BMI calculator using the following formula:Calculate the BMI using the following formula:

           w
 BMI =    ___
       (h/100) 2

where w is weight in kilograms and h is height in centimeters. Note that the denominator is squared.

Here is my code:

    /**
 * Calculates the Body Mass Index (BMI) for the user. The user must type in his
 * or her height in centimeters and weight in kilograms, and the computer prints
 * out the user's BMI.
 */
import java.util.Scanner; // import class
public class BMI {
    public static void main(String[] args) {
        int weight;
        int height;
        double bmi;
        Scanner console; //  create a variable to represent the console
        console = new Scanner (System.in); //  create the object with new
        System.out.print("How much do you weigh (in kg)? "); // user prompt to provide weight
        weight = console.nextInt(); // read from console
        System.out.print("How tall are you (in cm)? "); // user prompt to provide height
        height = console.nextInt(); // read value from console
        bmi = (double) weight / (height/100*height/100); // calculates BMI
        System.out.println("Your BMI is " + bmi); // displays user's BMI
    }
}

The program if one can call it that, runs but I think the calculation written incorrectly.

I've formatted the calculation several ways: bmi = (double) weight / (height/100*height/100); returns the weight EXCEPT when I use 100 for the weight and 200 for the height. I've tried the following:

bmi = (double)weight / height/100*height/100;

bmi = (double) weight / (height/100*height/100);

bmi = (double) weight / (height/100)*(height/100);

bmi = (double) weight / ((height/100)*(height/100));

bmi = (double)(weight / height/100*height/100);

bmi = (double) (weight / (height/100*height/100);

bmi = (double) (weight /(height/100)*(height/100);

bmi = (double)(weight) / ((height/100)*(height/100));

bmi = (double)(weight) / ((height/100)*(height/100));

bmi = (double)(weight) / height/100*height/100;

I either get the weight 100% of the time or it only works with 100 and 200 as the variables. I tried 75 and 150 and that also returns the weight.

At this point I don't even remember PEMDAS

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

2 Answers2

1

When you divide the int height by 100 it truncates the decimal since it is still an int.

Try initializing the variables as doubles:

double weight;
double height;

Then cast them when you get the int from the input:

weight = (double) console.nextInt();
height = (double) console.nextInt();

That way you're still taking an int for the input, but it acts like a double when you do your calculations.

1

Use a simplified formula to get desired result. Try this: bmc=(weight * 10000.0 ) /(height*height); No need for any casting as if there is any double number (10000.0) in expression then it auto returns result to double.

Akshay Khanna
  • 56
  • 1
  • 7