初始化变量

时间:2012-12-15 21:12:40

标签: java

该程序基本上要求用户输入他们的BMR和性别。然后他们要求选择一个活动级别(如果他们选择了错误的一个,我需要打印“请输入有效的选择”)。女性和男性有不同的活动因素。我遇到了“必须初始化变量ActivityFactor”的错误。我该如何修复我的代码?谢谢

        iBMR = new Scanner(System.in);
        Scanner iGender = new Scanner(System.in);
        Scanner iActivity = new Scanner(System.in);
        String AcFactor;
        double ActivityFactor ;
        double TDEE;
        //User Input
        System.out.println("Please enter your name: ");
        String inName = iName.next(); 
        System.out.println("Please enter your BMR: ");
        String inBMR = iBMR.next();
        double nBMR = Double.parseDouble(inBMR);
        System.out.println("Please enter your Gender");
        String inGender = iGender.next();

        System.out.println("Select Your Activity Level");
        System.out.println("[A] Resting (Sleeping,Reclining)");
        System.out.println("[B] Sedentary (Minimal Movement)");
        System.out.println("[C] Light (Sitting, Standing)");
        System.out.println("[D] Moderate (Light Manual Labor, Dancing, Riding Bike)");
        System.out.println("[E] Very Active (Team Sports, Hard Manual Labor)");
        System.out.println("[F] Extremely Active (Full-time Athlete, Heavy Manual Labor)");
        System.out.println("Enter the letter corrresponding to your activity level: ");
        String inActivity = iActivity.next();

        //CalculationFemale
        if(inActivity.equalsIgnoreCase("A")&& inGender.equalsIgnoreCase("F"))
        {
            ActivityFactor = 1.0;
        }
        else if(inActivity.equalsIgnoreCase("B")&& inGender.equalsIgnoreCase("F"))
        {
           ActivityFactor = 1.3;
        }
        else if(inActivity.equalsIgnoreCase("C")&& inGender.equalsIgnoreCase("F"))
        {
           ActivityFactor = 1.5;
        }
        else if(inActivity.equalsIgnoreCase("D")) && (inGender.equalsIgnoreCase("F"))
        {
           ActivityFactor = 1.6;
        }
        else if(inActivity.equalsIgnoreCase("E")&& inGender.equalsIgnoreCase("F"))
        {
           ActivityFactor = 1.9;
        }
        else if(inActivity.equalsIgnoreCase("F")&& inGender.equalsIgnoreCase("F"))
        {
           ActivityFactor = 2.2;
        }
        else
        {

            System.out.println("Please enter a valid choice!");
        }

        //CalculationMale
        if(inActivity.equalsIgnoreCase("A")&& inGender.equalsIgnoreCase("M"))
        {
            ActivityFactor = 1.0;
        }
        else if(inActivity.equalsIgnoreCase("B")&& inGender.equalsIgnoreCase("M"))
        {
           ActivityFactor = 1.3;
        }
        else if(inActivity.equalsIgnoreCase("C")&& inGender.equalsIgnoreCase("M"))
        {
           ActivityFactor = 1.6;
        }
        else if(inActivity.equalsIgnoreCase("D")&& inGender.equalsIgnoreCase("M"))
        {
           ActivityFactor = 1.7;
        }
        else if(inActivity.equalsIgnoreCase("E")&& inGender.equalsIgnoreCase("M"))
        {
           ActivityFactor = 2.1;
        }
        else if(inActivity.equalsIgnoreCase("F")&& inGender.equalsIgnoreCase("M"))
        {
           ActivityFactor = 2.4;
        }
        else
        {

           System.out.println("Please enter a valid choice!");
        }

        //TDEE
        double TD(ActivityFactor * BMR);

**谢谢你的回答。我确实意识到,如果所有if / if else条件都没有通过,那么“else”将没有ActivityFactor的值。感谢所有的回复,我现在意识到我追求这个的方式是行不通的。有人可以给我一个概述我如何编写这样一个程序,询问用户名/性别/ bmr,然后要求选择一个活动级别,并根据用户是男性/女性和他们的选择分配一个活动因子活动水平?谢谢

4 个答案:

答案 0 :(得分:0)

您只需要将ActivityFactor设置为某个虚拟值,以确保它已初始化:

double ActivityFactor = -1;

希望有所帮助!

答案 1 :(得分:0)

Java编译器发现您正在使用可能未初始化的变量:

double TD(ActivityFactor * BMR);

这是因为您有代码路径允许此变量保持单元化:

else
{

   System.out.println("Please enter a valid choice!");
}

因此,有两个选项:初始化您声明它的变量:

 double ActivityFactor = 0.0;

或者创建所有代码路径,以便在使用之前必须初始化此变量。在您的情况下,这意味着您需要将init添加到以下else块:

else
{
   ActivityFactor = 0.0;
   System.out.println("Please enter a valid choice!");
}

我建议你第一个选择。

答案 2 :(得分:0)

你必须在if-else语句之前给出一个值ActivityFactor。如果您的任何“如果”没有运行,则java无法在

知道ActivityFactor
double TD(ActivityFactor * BMR);

语句。

像这样声明和启动:

double ActivityFactor = 0.0;

答案 3 :(得分:0)

ActivityFactor必须在使用前初始化。您的代码不保证。最简单的方法是在声明中添加默认值:

double ActivityFactor = 10.0;