BMR计算器测试

时间:2015-10-28 06:45:28

标签: java math testing constructor calculator

刚刚完成了一个关于运动和BMR的项目的测试课,当我在指向第二个“BMR”的同时得到“无法找到符号”的错误时,同时让每个不同的人使用该测试。 AFAIK实际的bmr类工作正常,因为它没有给我任何错误。

考试类:

public class BMRTest {
  public static void main(String[] args) {
    BMR bmr1 = new BMR("Test Person1", 123, 128, 30, 'm', 2);
    BMR bmr1 = new BMR("Test Person2", 156, 115, 24, 'f', 1);
    BMR bmr1 = new BMR("Test Person3", 136, 147, 19, 'm', 5);
    BMR bmr1 = new BMR("Test Person4", 145, 192, 24, 'm', 4);
    BMR bmr1 = new BMR("Test Person5", 125, 121, 44, 'f', 3);

    System.out.println("1" + bmr1.calculateBMRWithExercise());
    System.out.println("2" + bmr1.calculateBMRWithExercise());
    System.out.println("3" + bmr1.calculateBMRWithExercise());
    System.out.println("4" + bmr1.calculateBMRWithExercise());
    System.out.println("5" + bmr1.calculateBMRWithExercise());
  }
}

实际BMR等级:

public class BMR {

  private String Name;
  private float weight;
  private float height;
  private int age;
  private char gender;
  private int exercise; //1-5

  //BP
  public BMR(String n, float w, float h, int a, char g, int e) {
    n = Name;
    w = weight;
    h = height;
    a = age;
    g = gender;
    e = exercise; //1-5
  }

  //CALCULATE
  public float calculateBMR() {
    float rv;

    if (gender == 'f') {
      rv = femaleBMR();
    } else {
      rv = maleBMR();
    }
    return rv;
  }

  //MALE BMR
  private float maleBMR() {
    return 66 + 6.23f * weight + 4.7f * height - 6.8f * age;
  }

  //FEMALE BMR
  private float femaleBMR() {
    return 655 + 4.35f * weight + 12.7f * height - 4.7f * age;
  }

  //EXERCISE ADJUSTMENT
  public float calculateBMRWithExcercise() {
    float rv = 0;
    float bmr = calculateBMR();

    switch (exercise) {
      case 1:
        rv = bmr * 1.2f;
        break;

      case 2:
        rv = bmr * 1.375f;
        break;

      case 3:
        rv = bmr * 1.55f;
        break;

      case 4:
        rv = bmr * 1.725f;
        break;

      case 5:
        rv = bmr * 1.9f;
        break;

      default:
        System.out.println("Error");
        break;
    }
    return rv;
  }
}

2 个答案:

答案 0 :(得分:0)

在测试代码中,您尝试多次声明同一个对象。尝试为每个测试用例创建一个单独的对象,如下所示:

public static void main(String[] args) {
    BMR bmr1 = new BMR("Test Person1", 123, 128, 30, 'm', 2);
    BMR bmr2 = new BMR("Test Person2", 156, 115, 24, 'f', 1);
    BMR bmr3 = new BMR("Test Person3", 136, 147, 19, 'm', 5);
    BMR bmr4 = new BMR("Test Person4", 145, 192, 24, 'm', 4);
    BMR bmr5 = new BMR("Test Person5", 125, 121, 44, 'f', 3);

    System.out.println("1" + bmr1.calculateBMRWithExercise());
    System.out.println("2" + bmr2.calculateBMRWithExercise());
    System.out.println("3" + bmr3.calculateBMRWithExercise());
    System.out.println("4" + bmr4.calculateBMRWithExercise());
    System.out.println("5" + bmr5.calculateBMRWithExercise());
}

答案 1 :(得分:0)

在BMRTest主方法中,您应该创建5个具有5个单独名称的BMR对象。相反,你用名称" bmr1"声明它们全部,这可能导致错误。尝试将其更改为以下内容:



public class BMRTest {
  public static void main(String[] args) {
    BMR bmr1 = new BMR("Test Person1", 123, 128, 30, 'm', 2);
    BMR bmr2 = new BMR("Test Person2", 156, 115, 24, 'f', 1);
    BMR bmr3 = new BMR("Test Person3", 136, 147, 19, 'm', 5);
    BMR bmr4 = new BMR("Test Person4", 145, 192, 24, 'm', 4);
    BMR bmr5 = new BMR("Test Person5", 125, 121, 44, 'f', 3);

    System.out.println("1" + bmr1.calculateBMRWithExercise());
    System.out.println("2" + bmr2.calculateBMRWithExercise());
    System.out.println("3" + bmr3.calculateBMRWithExercise());
    System.out.println("4" + bmr4.calculateBMRWithExercise());
    System.out.println("5" + bmr5.calculateBMRWithExercise());
  }
}