Java使用else if来分配值

时间:2016-09-15 02:47:29

标签: java

我是编程和Java的新手。如果他们选择了正确的输入,我试图使用else if为变量赋值。但每当我尝试编译它时,它都说无法找到变量。

import java.util.Scanner;

public class TDEE
{
    public static void main(String[] args)
    {

    Scanner in = new Scanner(System.in);

    double tdee = 0.0;

    System.out.print("Please enter your name: ");
    String name = in.next();

    System.out.print("Please enter your BMR: ");
    double bmr = in.nextDouble();

    System.out.print("Please enter your Gender (M/F): ");
    String gender = in.next();
    System.out.println();

    // providing menu items
    System.out.println("Select your activity level: ");
    System.out.println("[0] Resting (Sleeping. Reclining)");
    System.out.println("[1] Sedentary (Minimal Movement)");
    System.out.println("[2] Light (Sitting, Standing)");
    System.out.println("[3] Moderate (Light Manual Labor, Dancing, Riding Bike)");
    System.out.println("[4] Very Active (Team Sports, Hard Manual Labor)");
    System.out.println("[5] Extremely Active (Full-time Athlete, Heavy Manual Labor)");
    System.out.println();

    // accept user choice with a Scanner class method
    System.out.print("Enter the number corresponding to your activty level(0,1,2,3,4, or 5): ");
    String choice = in.next();
    System.out.println();

    if(choice.equalsIgnoreCase("0"))
    {
        double activityFactor = 1.0;
    }

    else if(choice.equalsIgnoreCase("1"))
    {
        double activityFactor = 1.3;
    }

    else if(choice.equalsIgnoreCase("2") && "M".equals(gender))
    {
        double activityFactor = 1.6;
    }

    else if(choice.equalsIgnoreCase("2") && "F".equals(gender))
    {
        double activityFactor = 1.5;
    }

    else if(choice.equalsIgnoreCase("3") && "M".equals(gender))
    {
        double activityFactor = 1.7;
    }

    else if(choice.equalsIgnoreCase("3") && "F".equals(gender))
    {
        double activityFactor = 1.6;
    }

    else if(choice.equalsIgnoreCase("4") && "M".equals(gender))
    {
        double activityFactor = 2.1;
    }

    else if(choice.equalsIgnoreCase("4") && "F".equals(gender))
    {
        double activityFactor = 1.9;
    }

    else if(choice.equalsIgnoreCase("5") && "M".equals(gender))
    {
        double activityFactor = 2.4;
    }

    else if(choice.equalsIgnoreCase("5") && "F".equals(gender))
    {
        double activityFactor = 2.2;
    }

    else
    {
        System.out.println("You did not choose a valid manu option.");
    }

    tdee = bmr * activityFactor;

    System.out.println();
    System.out.println("Name: " + name + "      Gender: " + gender);
    System.out.println("BMR: " + bmr + "       Activity Factor: " + activityFactor);
    System.out.println("TDEE: " + tdee);
    }
}

1 个答案:

答案 0 :(得分:4)

它没有编译的原因是因为范围界定。您声明的变量位于if语句中。这使得它的范围,访问它的能力,仅在if语句中。要在if语句之外引用它,必须在要访问它的范围内声明它。然后,您可以在activityFactor = 1.0;语句中为其分配值,如下所示if else。声明变量的地方决定了可以访问它们的内容。

您必须在double activityFactor语句之外声明if else。只需在语句上方添加double activityFactor;,并将所有double activityFactor替换为activityFactor,它就应该编译。