BMI calcuator, several methods, code check

martines :

It's my first program with several methods

1st has to convert height to inches

2nd calculate BMI

3rd receive BMI and and return the status

4th which is the main, has to call for input and generate output

The problem is that it doesnt calculate the BMI - it outputs 0. When I run it in just one method, it works fine. What might be wrong?

package bmiCalculator;
java.util.Scanner;
public class BmiCalculator {

public static double bmi;
public static int height;
public static int feet;
public static int inches;
public static int weight;
public static String status;


  public static void convertToInches (){

    height = feet * 12 + inches;

 }
  public static void bmiCalculator (){

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


}
    public static void weightStatus () {



        if (bmi < 18.5){
           status = "underweight"; 
         }
        else if (bmi <= 24.9){
            status = "normal";
        }
        else if (bmi <= 29.9){
            status = "overweight";
        }
        else if (bmi >= 30){
            status = "obese";
        }
    }

    public static void main (String[] args){

       System.out.println("Put your height in ft and inches"); 
       Scanner sc = new Scanner(System.in); 
       feet = sc.nextInt();
       inches = sc.nextInt();

       System.out.println("Put your weight in pounds");
       weight = sc.nextInt();


       System.out.println("Height: " + feet + " feet, " + inches + " inches");
       System.out.println("Weight: " + weight + " pounds");
       System.out.println("Your BMI is " + bmi + "category" + status);

    }
 }
ruhul :

Declaring those method does not mean all will execute. you need to call those methods from main accordingly.

for example:

   ...
   System.out.println("Put your weight in pounds");
   weight = sc.nextInt();


   System.out.println("Height: " + feet + " feet, " + inches + " inches");
   System.out.println("Weight: " + weight + " pounds");
   // call corresponding method to calculate:

   convertToInches();
   bmiCalculator();
   weightStatus();
   // now all of those method are executed.

   System.out.println("Your BMI is " + bmi + "category" + status);

Declaring all those methods and properties as static is not a good practice. Please learn how OOP works.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129214&siteId=1