代码之家  ›  专栏  ›  技术社区  ›  user8107351

在Java中,体重和身高是一个数组,如何计算BMI

  •  0
  • user8107351  · 技术社区  · 8 年前

    我在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("");
    

    所以我必须在名字数组中找到“弗雷德里克”,然后计算他的体重指数。任何想法都会受到赞赏。

    3 回复  |  直到 8 年前
        1
  •  0
  •   Rafał Sokalski    8 年前

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

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

    但这是有风险的,因为如果names数组的长度大于weight或height,它将抛出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]));
            }
    
        }
    }
    

        2
  •  0
  •   László Stahorszki    8 年前

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

    在这个类中,编写一个返回BMI的方法非常简单。要创建这些实例,可以使用类似于上面答案的方法,但是,我建议您进行额外的检查,检查数组是否具有相同的大小。

        3
  •  0
  •   MC Emperor    8 年前

    首先,我不打算为您编写代码,我认为您最终应该自己编写。

    • 作业不太准确。上面写着 “计算并打印每个用户的BMI(体重指数)” equalsIgnoreCase 方法,建议您只需要显示给定用户的BMI。
    • for 环在循环中,检查名称是否正确。您不需要嵌套循环,为什么要使用嵌套循环?
    • 您可以断言所有给定的数组长度相等,因为赋值没有提到数组长度。您可能需要重新检查 familyNames ,因为它比其他数组短。
    • 你呢 想创建一个新的方法来计算实际的体重指数,并从 calculateBMI 方法:

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

      把实际计算和描述体重指数分开。

      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" };