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