我如何在Java中计算体重指数(体重和身高是一个数组)

时间:2018-11-07 07:32:19

标签: java arrays loops

我在Java中有一个作业,我必须在一个用户数组,一个高度数组和一个权重数组中计算特定用户的BMI。

以下是作业:

  String[] names; // users names
                String[] familyNames; // familyNames
                int[] weight; // users weights in kilograms (kg)
                double[] height; // users height in meter (m)


                names = new String[] { "Alex", "Maria", "Anna", "Adam", "Sara", "Johan", "Frederik"};
                familyNames = new String[] {"Andersson", "Johansson", "Nordin", "Holmgren", "Svensson"};
                weight = new int[] {70, 50, 60, 50, 60, 87, 130 };
                height = new double[] { 1.80, 1.70, 1.57, 1.80, 1.69, 1.85, 1.85 };

        public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {


                /*
                 * This method should be changed
                 * 
                 * Calculate and print out BMI (body mass index) for each user 
                 * BMI = weight in kg/height in meter * height in meter 
                 * 
                 * Check if the user is Underweight, Normal, Overweightor or Obese based on the BMI
                 * Underweight, when bmi is less than 18.5 
                 * Normal, when bmi is between 18.5 and 24.9 
                 * Overweight, when bmi is between 25 and 29.9
                 * Obese, when bmi is more than 30
                 * 
                 * 
                 * To check if a string is equal to another string use:
                 * stringVariable.equalsIgnoreCase(anotherStringVariable)
                 * 
                 */

            }

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

        /*
         * This method should be changed
         * 
         * Calculate and print out the weight that the user has to loose or gain
         * 
         * Let's consider that the formula for ideal body weight is (Broca Index:):
         * Ideal Body Weight (kg) = (Height (cm) - 100) - ((Height (cm) - 100) x 10%)
         * 
         * 1 meter = 100 cm
         * 
         * 
         */
        //Your code starts here


    }// end calculateWeightLoss

我尝试这样做:

double userBMI = 0;
   for(int i = 0; i < weight.length; i++){
                for(int j = 0; j < height.length; j++){
                    userBMI = weight[i]/height[j] * height[j];
                }
            }

但是我觉得我没有走上正轨,因为使用的方法是:

calculateBMI("Frederik",names, weight,height); // calculate user BMI
        calculateWeight("Frederik", names, weight, height); // calculate the weight loss or gain for the user
        System.out.println("");

因此,我必须在名称数组中查找“ frederik”,然后计算其BMI。任何想法都会受到赞赏。

3 个答案:

答案 0 :(得分:0)

当传递给与“名称”数组中的名称匹配的方法名称时,它将打印BMI

public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if(names[i].equalsIgnoreCase(name))
            System.out.println(weight[i] / height[i] / height[i]);
    }
}

但这是有风险的,因为如果名称数组的长度大于权重或高度,则会抛出ArrayIndexOutOfBoundsException,但我认为数组始终具有适当的长度。如果这些数组的长度可能不同,我建议添加一些条件

第二种方法:

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if (names[i].equalsIgnoreCase(name)) {
            double perfectWeight = ((height[i] * 100) - 100) - (((height[i] * 100) - 100) / 10);
            System.out.println("Perfect weight for that height is : " + perfectWeight);
            if(weight[i] == perfectWeight)
                System.out.println("Person has perfect weight");
            else if(weight[i] > perfectWeight)
                System.out.println("Person should lose: " + (weight[i] - perfectWeight));
            else
                System.out.println("Person should gain: " + (perfectWeight - weight[i]));
        }

    }
}

首先计算最理想的体重,然后将其与人的体重进行比较,并打印人的体重或体重增加多少

答案 1 :(得分:0)

我建议您编写一个类,我们称其为Person。在此类的每个实例中,您可以存储给定人员的姓名,身高和体重。

class Person {
    private String name;
    private String familyName;
    private int weight;
    private double height;
}

在此类中,编写返回BMI的方法非常简单。要创建这些实例,可以使用与上述答案类似的方法,但是,我建议您进行附加检查,以检查数组的大小是否相同。

答案 2 :(得分:0)

首先,我不会为您编写该代码,我认为您最终应该自己完成。

以下是一些提示:

  • 分配不太准确。它说“为每个用户计算并打印出体重指数(BMI)” ,但是随后它要求您实现一个接受特定名称作为第一个参数的方法。描述中提到了equalsIgnoreCase方法,这表明您只需要显示该给定用户的BMI。
  • 因此,您需要某种循环,也许是for循环。在循环中,检查名称是否正确。您不需要嵌套循环,为什么要使用嵌套循环?
  • 您可以断言所有给定数组的长度均相等,因为赋值未提及数组长度。您可能要重新检查familyNames的长度,因为它比其他数组短。
  • 可能想要创建一个新的方法来执行实际的BMI计算,并从calculateBMI方法中引用它:

    private static double calculateActualBmi(int weigth, double height) {
        return ...; // perform calculation
    }
    

    并将实际计算与描述BMI分开。

    private static String describeBmi(double bmi) {
        if (bmi < 18.5) {
            return "Underweight";
        }
        else if ...
            ...
        else {
            return "Obese";
        }
    }
    
  • 另一种方法的工作方式相同。


更多笔记

  • 我很惊讶分配需要您这样编写方法calculateBMI(String name, String[] names, int[] weight, double[] height)。这种设计错过了面向对象编程的全部要点。参见László Stahorszki's answer

  • 您可以一次声明并初始化数组,即

    String[] names = new String[] { "Alex", ..., "Frederik" };