简体   繁体   中英

Initializing variable

The program basically ask user to input their BMR, and gender. Then they're ask to choose an activity level ( If they choose the wrong one, I need to print" Please enter a valid choice"). Female and male have different activity factor. I'm encountering an error of " Variable ActivityFactor must be initialized." How can I fix my code? Thanks

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

**Thanks you for all the answer. I do realize that if all the if/if else conditions do not go through, the " else" won't have a value for ActivityFactor. Thanks to all the reply, I now realize my way of pursuing this won't work. Could someone give me an outline of how I can code such a program that ask for the user name/gender/bmr then ask to select a activity level and assign an activity factor depending on whether the user is a male/female and their selection of activity level ?Thanks

You just need to set ActivityFactor to some dummy value to make sure it's initialized:

double ActivityFactor = -1;

Hope that helps!

Java compiler identified that you are using potentially uninitialized variable:

double TD(ActivityFactor * BMR);

This is because you have code paths which allow this variable to stay unitialized:

else
{

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

So, there are two options: initialize this variable where you declare it:

 double ActivityFactor = 0.0;

Or make all your code paths so that this variable must be initialized for sure before it is using. In your case it means you need to add init to the following else block:

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

I suggest you the first option to go.

You have to give a value ActivityFactor before if-else statements. If any of your 'if's do not operated, java can not know ActivityFactor at

double TD(ActivityFactor * BMR);

statement.

declare and initilaze like this:

double ActivityFactor = 0.0;

ActivityFactor have to be initialized before use. Your code don't guarantee that. The simplest way is to add default value on declaration:

double ActivityFactor = 10.0; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM