试图用循环计算BMI?

时间:2018-02-16 15:07:17

标签: java methods return

我必须做一个程序,循环BMI为五个人,我使用扫描仪输入名称,重量和身高,但我似乎无法返回输出。我正在使用多种方法。

该程序应该如下所示:1

我所拥有的就是:

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int i;

        System.out.println("How many BMIs do you want to compute? " + 5);

        for (i = 1; i <= 5; ++i) {

            System.out.println("\n\nLoop " + i + " out of " + 5);

            System.out.print("Client's first name: ");
            String firstName = sc.next().toLowerCase();

            System.out.print("Client's weight in pounds: ");
            double weight = sc.nextDouble();

            System.out.print("Client's height in inches: ");
            double height = sc.nextDouble();

            return;
        }



        sc.close();
    }


    public static double computeBMI(double weight, double height) {

        double bmi = ((weight * 703) / Math.pow(height, 2));

        if (bmi < 18.5 ) {
            System.out.println("underweight.\n");
        }
        else if ( (bmi >= 18.5) && (bmi < 25) ) {
            System.out.println("normal weight.\n");
        }
        else if ( (bmi >= 25) && (bmi < 30) ) {
            System.out.println("overweight.\n");
        }
        else {
            System.out.print("obese.\n");
        }

        return bmi;

    }


    public static String printResult(String firstName, double bmi) {

        double weight;
        double height;

        System.out.printf("\nDear %s, your BMI is %.1f and you are ", firstName, bmi);

我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

main替换为以下内容。正如您当前的代码所代表的那样,您甚至没有限制您定义的方法。

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 int i;

 System.out.println("How many BMIs do you want to compute? " + 5);

 for (i = 1; i <= 5; ++i) {

  System.out.println("\n\nLoop " + i + " out of " + 5);

  System.out.print("Client's first name: ");
  String firstName = sc.next().toLowerCase();

  System.out.print("Client's weight in pounds: ");
  double weight = sc.nextDouble();

  System.out.print("Client's height in inches: ");
  double height = sc.nextDouble();

  printResult(firstName, (computeBMI(wight, height))); //HERE
 }
 sc.close();
 return;
}