Home > Net >  Turning an Integer into a Decimal
Turning an Integer into a Decimal

Time:01-03

Slightly confused on how i can create this code for a BMI calculator.

I have been asked to input 2 integers each for height and weight. It will be presented in the below format with the ranges.

Enter Height in Feet and Inches
Enter Feet(2-7): --
Enter Inches(0-11): --

Enter Weight in Stones and Pounds
Enter Stones(3-30): --
Enter Pounds(0-13):--

This data would then be added into a function that calculates the BMI

BMI = weight * 703 / height2

I was wondering how i can add a decimal place in between the two integers for each height and weight. I was first thinking of just adding the numbers and dividing by 10, but that is obviously wrong. or using the printf function to print it on screen. But then it is still wrong in the formula if im right?

Scanner input = new Scanner(System.in);
System.out.print("Enter feet: ");
int feet  = input.nextInt();
        
System.out.print("Enter inches: ");
int inches  = input.nextInt();
        
System.out.print("Enter stone: ");
int stone  = input.nextInt();
        
System.out.print("Enter pounds: ");
int pounds  = input.nextInt();

CodePudding user response:

Well there's a few ways to do this. But I don't think that's what you need to do in this scenario. For instance, someone saying they are 5'11", simply adding a decimal place will result in the value 5.11 feet which is incorrect, as 5'11" is actually 5.9167 feet.

5 11 ÷ 12 ≈ 5.9167 feet

Assuming you're using this chart, What you need to do here is multiply feet by 12 to get the feet in inches, and add this value to the inches input to get the total height in inches. Similarly, for stones, you would multiply the stones value by 14 to convert to pounds, and add it to the inputted value for pounds to get the total weight in pounds. This does not require adding a decimal point between two values.

  •  Tags:  
  • java
  • Related