2016-09-15 3 views
-4

私はプログラミングとJavaについてとても新しいです。彼らが正しい入力を選択した場合、変数に値を代入する場合はelseを使用しようとしています。しかし、コンパイルしようとするたびに変数を見つけることができないと言います。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); 
    } 
} 
+0

[構文質問IF ELSE(Java)]の可能な複製(http://stackoverflow.com/questions/1496435/syntax-question-if-else-java) – Nikhil

答えて

4

コンパイルしていないのは、スコープが原因です。宣言している変数はifステートメント内にあります。それはその範囲、それにアクセスする能力を、ifの声明の中でのみ与える。 ifステートメントの外側で参照するには、アクセスするスコープ内で宣言する必要があります。次に、if elseステートメント内で、activityFactor = 1.0;のように値を割り当てることができます。変数を宣言する場所によって、変数にアクセスできるものが決まります。

if elseステートメントの外にdouble activityFactorを宣言する必要があります。文の上にdouble activityFactor;を含めて、double activityFactorをすべてactivityFactorに置き換え、コンパイルする必要があります。

関連する問題