-2

I am having issues displaying the result for a calculation of Body Mass Index. I am uncertain as to why the bodyMassIndex variable is not performing the calculation and displaying the result. Thanks for any feedback.

// Write a Java program to compute body mass index (BMI).

public class Exercise3 {
  public static void main (String [] args) {

// Variable Declaration
int height;
int weight;
double bodyMassIndex;
Scanner input = new Scanner (System.in);

// Obtain user input
System.out.println("What is your remaining height in inches: ");
height = input.nextInt();

System.out.println("What is your weight (in pounds): ");
weight = input.nextInt();

// Perform computations

bodyMassIndex = (weight / (height * height)) * 703;

// Display Results
System.out.println("Your Body Mass Index is: " + bodyMassIndex);

  }
}
yole
  • 92,896
  • 20
  • 260
  • 197
Rye Guy
  • 45
  • 2
  • 4
    _why the bodyMassIndex variable is not performing the calculation and displaying the result_ -- what do you expect to happen, and what is happening? Why do you think that is? Have you tried defining `height` and `weight` as `double` instead (so you're not just doing integer arithmetic, then assigning to a double) – Krease Mar 20 '18 at 18:16

1 Answers1

1

You should type cast all the integers to doubles so that you can have your result.

In fact this will work for you

bodyMassIndex = ((double)weight / (height * height)) * 703;

catchiecop
  • 388
  • 6
  • 24
  • 2
    This works, but you only needs the `double` casting on `weight` -- all the other is superfluous – Krease Mar 20 '18 at 18:22