无法识别的Java变量

时间:2018-06-02 13:36:01

标签: java variables

我正在编写一种方法来对一个数字进行因式分解,出于某些原因,当我尝试打印factorA时,它表示它无法识别,是否有任何建议?

    public static void factorize(int n){
    if (n % 2 == 0){                                                //If number is even
        int factorA = 2;
        int factorB = n/2;
    }else{                                                          //If number is odd
        int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
        int smallestFactorOdd = 3;

        while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
            if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
                int factorA = smallestFactorOdd;
                int factorB = n/smallestFactorOdd;
            }else{
                smallestFactorOdd += 2;
            }
        }
    }
    System.out.println(factorA);
}

更新的代码(仍然接收错误):

public static void factorize(int n){
    int factorA;
    int factorB;

    if (n % 2 == 0){                                                //If number is even
        factorA = 2;
        factorB = n/2;
    }else{                                                          //If number is odd
        int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
        int smallestFactorOdd = 3;

        while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
            if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
                factorA = smallestFactorOdd;
                factorB = n/smallestFactorOdd;
            }else{
                smallestFactorOdd += 2;
            }
        }
    }
    System.out.println(factorA);
    System.out.println(factorB);
}

3 个答案:

答案 0 :(得分:3)

您在factorA循环中声明while。在循环内声明的任何变量都不存在于循环之外。 (其他类和方法甚至不知道变量存在)您需要在循环之外声明变量

像这样:

public static void factorize(int n){
    int factorA = 0;
    int factorB = 0;
    ...

编辑:

声明时,必须将变量设置为零。否则,它有可能永远不会被赋予价值。由于它是方法局部变量,因此必须在使用之前对其进行初始化。 (然后你尝试打印它。编译器不知道值,所以它抱怨)

进一步阅读第二个错误:

Default Values and Initialization in Java

答案 1 :(得分:0)

您必须在使用本地变量之前对其进行初始化。

只有在实例变量的情况下才需要初始化基元。

实例变量在创建该类的对象时初始化。但是必须初始化方法范围中定义的变量。

因此,在您的方法中,将0分配给您的本地变量,它会起作用。

public static void factorize(int n){
int factorA = 0;
int factorB = 0;

if (n % 2 == 0){                                                //If number is even
    factorA = 2;
    factorB = n/2;
}else{                                                          //If number is odd
    int halfLine = n/2;                                         //Only variables that are even will be factors greater than half of them, this case is only for odd integers
    int smallestFactorOdd = 3;

    while (smallestFactorOdd < halfLine){                       //Until you've found the smallest factor for add variables
        if (n % smallestFactorOdd == 0){                        //If smallestFactorOdd is a factor of n
            factorA = smallestFactorOdd;
            factorB = n/smallestFactorOdd;
        }else{
            smallestFactorOdd += 2;
        }
     }
  }
  System.out.println(factorA);
  System.out.println(factorB);
}

答案 2 :(得分:0)

声明和初始化都必须在if

之外
public static void factorize(int n){
    int factorA = 0;
    int factorB = 0;

    if (n % 2 == 0){                                                 
        factorA = 2;
        factorB = n/2;
    }else{                                                           
        int halfLine = n/2;                                         
        int smallestFactorOdd = 3;
相关问题