卡路里计划 - Java计算

时间:2015-03-02 13:14:12

标签: java

此代码的问题是计算所需的每日卡路里。无论是男性还是女性,我都得不到合适的数量。如果我可以改进任何事情,我都会听到。谢谢。我是java的初学者。

public static void main(String[] args) 
    {

        //variables
        int age;
        int height;
        int weight;
        String gender;
        int BMR;
        char genderChar;
        boolean male;
        String exercise;
        boolean none = false;
        boolean light = false;
        boolean moderately = false;
        boolean intensely = false;
        boolean five;
        double cal;




        //Male or Female?
        Scanner keyboard = new Scanner(System.in);

        System.out.println("What is your gender? M or F: ");
        gender = keyboard.nextLine(); 

        //Determining your BMR 

        System.out.println("What is your age: ");
        age = keyboard.nextInt();

        System.out.println("What is your weight: ");
        weight = keyboard.nextInt();

        System.out.println("What is your height: ");
        height = keyboard.nextInt();

        genderChar = gender.charAt(0);

        male = genderChar == 'M';

        if (male)
        {
            BMR = (int) (66 + (6.23 * weight) + (12.7 * height) - (6.8 * age));
        }
        else
        {
            BMR = (int) (665 + (4.35 * weight) + (4.7 * height) - (4.7 * age));
        }

        //Show BMR

        if (male)
        {
            System.out.println("Your BMR is " + BMR);
        }
        else
            System.out.println("Your BMR is " + BMR);

        //Level of Exercises

        if (none)
        {
            cal =  (BMR * 1.2);
        }
        else if (light)
        {
            cal = (BMR * 1.375);
        }
        else if (moderately)
        {
            cal = (BMR * 1.55);
        }
        else if (intensely)
        {   
            cal = (BMR * 1.725);
        }
        else
        {
            cal = (BMR * 1.9);
        }

        System.out.println("What is your level of exercise? ");
        System.out.println("Type in none if you do not exercise. ");
        System.out.println("Type in 2 if you engage in light exercise one to three days a week.");
        System.out.println("Type in 3 if you do exercise moderately three to five times a week.");
        System.out.println("Type in 4 if you do intensely six to seven days a week.");
        System.out.println("Type in 5 if you do exercise intensely six to seven days a week and have a physically active job.");

        exercise = keyboard.nextLine();
        none = keyboard.nextLine() != null;

        System.out.println("Your daily calorie needs " + cal);
    }

}

2 个答案:

答案 0 :(得分:1)

一些事情:

  • 您的代码male = genderChar == 'M';需要更改为male = genderChar.equalsIgnoreCase("M");因为来自扫描程序的nextLine将返回字符串而不是字符并且比较字符串您需要双引号用于常量文字并匹配m或M(案例在敏感匹配中),您需要equalsIgnoreCase方法。
  • 如果出现以下情况,则无需关注:

    if (male)
    {
        System.out.println("Your BMR is " + BMR);
    }
    else
        System.out.println("Your BMR is " + BMR);
    

说完

System.out.println("Your BMR is " + BMR);

- 您的nonelight等可变对象在您将其初始化为false后永远不会发生变化。因此,您可能希望根据您的要求适当地设置它们。

答案 1 :(得分:1)

在您从中获取输入之前,您正在计算cal值    用户。这样,所有布尔将被设置为" false"这意味着你的    代码转到else块,只计算cal = (BMR * 1.9);。另外,我认为你应该在这个例子中使用switch-case。

    System.out.println("What is your level of exercise? ");
    System.out.println("Type in none if you do not exercise. ");
    System.out.println("Type in 2 if you engage in light exercise one to three days a week.");
    System.out.println("Type in 3 if you do exercise moderately three to five times a week.");
    System.out.println("Type in 4 if you do intensely six to seven days a week.");
    System.out.println("Type in 5 if you do exercise intensely six to seven days a week and have a physically active job.");

    exercise = keyboard.nextLine();

    //Use a switch-case now
    switch(exercise) {
    case "none":
      //calculate cal here
      break; //Don't forget the break!!
    case "2":
      //calculate cal here
      break; //Don't forget the break!!
    case "3":
      //calculate cal here
      break; //Don't forget the break!!
    //continue with the rest here
    }

供参考,请使用this tutorial