BMI计算器,几种方法,代码检查

这是我的第一个有几种方法的程序


第一个必须将高度转换为英寸


2、计算BMI


第三次接收 BMI 并返回状态


第 4 个是主要的,必须调用输入并生成输出


问题是它不计算 BMI - 它输出 0。当我只用一种方法运行它时,它工作正常。可能出了什么问题?


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);


    }

 }


紫衣仙女
浏览 158回答 2
2回答

Helenr

声明这些方法并不意味着所有方法都会执行。您需要main相应地调用这些方法。例如:&nbsp; &nbsp;...&nbsp; &nbsp;System.out.println("Put your weight in pounds");&nbsp; &nbsp;weight = sc.nextInt();&nbsp; &nbsp;System.out.println("Height: " + feet + " feet, " + inches + " inches");&nbsp; &nbsp;System.out.println("Weight: " + weight + " pounds");&nbsp; &nbsp;// call corresponding method to calculate:&nbsp; &nbsp;convertToInches();&nbsp; &nbsp;bmiCalculator();&nbsp; &nbsp;weightStatus();&nbsp; &nbsp;// now all of those method are executed.&nbsp; &nbsp;System.out.println("Your BMI is " + bmi + "category" + status);将所有这些方法和属性声明为静态并不是一个好的做法。请了解OOP工作原理。

呼如林

首先,您需要在用户输入这样的值后调用这些方法。...&nbsp; &nbsp;System.out.println("Put your weight in pounds");weight = sc.nextInt();convertToInches();bmiCalculator();weightStatus();System.out.println("Height: " + feet + " feet, " + inches + " inches");...调用方法的顺序很重要,因为它们之间存在依赖关系。此外,您需要在 BMI 除法之前将除数和被除数转换为双倍,因为 int/int = int 和 java 舍入该值。public static void bmiCalculator() {&nbsp; &nbsp; bmi = (double)(weight * 703) / (double)(height * height);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java