【问题标题】:Unrecognizable Java Variable无法识别的 Java 变量
【发布时间】:2018-11-12 10:12:00
【问题描述】:

我正在编写一种方法来分解一个数字,由于某种原因,当我尝试打印 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);
}

【问题讨论】:

  • 现在有什么新错误??还是同样的老错误??
  • 提示:您想了解“范围界定”。在两个不同的子作用域中声明变量不会在封闭作用域中创建它!
  • 错误表示变量可能尚未声明,我将研究范围

标签: java variables


【解决方案1】:

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

像这样:

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

编辑:

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

关于第二个错误的进一步阅读:

Default Values and Initialization in Java

【讨论】:

  • 我在循环外初始化了变量,但仍然收到错误,它说变量可能没有被声明。
  • @Pagaley12 你声明factorA 时是否设置为0?
  • @GBlodgett 您不必初始化原始类型 AFAIK。
  • @Nathan 因为facotrA是一个方法局部变量,所以在使用前需要初始化
【解决方案2】:

声明和初始化都必须在 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;

【讨论】:

    【解决方案3】:

    你必须在使用之前初始化局部变量。

    您不需要仅在实例变量的情况下初始化原语。

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

    因此,在您的方法中,将 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 2021-02-27
      • 2013-05-15
      • 2019-12-17
      • 1970-01-01
      • 2014-06-10
      • 2017-01-04
      • 2013-08-13
      相关资源
      最近更新 更多