简体   繁体   English

for循环内部的变量是局部的,我想将其公开

[英]Variable inside for loop is local, I want to make it public

if (a != 1 && solone == (int)solone && soltwo == (int)soltwo){
    // (lx+o)(mx+p)
    int h = (a*c);
    List<Integer> factors = new ArrayList<Integer>();
    for (int i = 1; i < Math.sqrt(h); i++) {
        if (h % i == 0)
            factors.add(i);
    }
    Integer result = null;
    for (int ii: factors) {
        if (b == ii + h/ii){
            result = ii;
            // ax^2+hiix+iix+c
    }
    int hii = h/ii;
    int gcd1 = Euclid.getGcd(a, hii);
    int gcd2 = Euclid.getGcd(ii, c);
    String Factored = FactoredForm.getFF(gcd1, gcd2, a, hii);
}

My String called Factored is one I need to use for printing later in my code. 我的称为Factored的字符串是我以后在代码中用于打印的字符串。 I can't use it because it doesn't recognize the variable outside of the for loop. 我无法使用它,因为它无法识别for循环之外的变量。 How do I go about making it public? 我如何将其公开? When i added public in front of the string, it said that only final is permitted? 当我在字符串前面添加public时,它说只允许final吗? Also, I cannot simply move the extraneous code outside of the for loop because it all depends on the integer "ii" which is part of the loop. 另外,我不能简单地将多余的代码移到for循环之外,因为它全部取决于整数“ ii”,它是循环的一部分。 Help! 救命!

Do you really want this to be part of the state of an instance of the class? 您是否真的希望将其作为类实例状态的一部分? If so, declare it outside the method: 如果是这样,则在方法之外声明它:

private string factored;

public void Whatever(...)
{
    factored = FactoredForm.getFF(gcd1, gcd2, a, hii);
}

I would advise you not to make it public. 我建议您不要公开。 If you need to expose the value, do so via a property. 如果需要公开该值,请通过一个属性来公开。

Think carefully about whether it really is logically part of the state of this class though. 但是,请仔细考虑它是否确实在逻辑上属于此类的状态。 Also revisit naming conventions, as mentioned before. 如前所述,还要重新访问命名约定。

public attribute is not related to a local variable but to an instance variable . public属性与局部变量无关,而与实例变量相关

Inside the same function declarations follow two rules: 在相同的函数声明中,遵循两个规则:

  • the order of declaration : if a local variable hasn't been declared yet, then you can't use it. 声明顺序 :如果尚未声明局部变量,则不能使用它。
  • the scoping : if a variable has been declared inside a scope ( { ... } ) then you can't access it from outside the scope. 作用域 :如果在范围( { ... } )中声明了变量,则无法从范围外部访问它。

If you want to access the variable later in the code you should declare it before the loop: 如果要稍后在代码中访问变量,则应在循环之前声明它:

String factored;
if (....) {
  ....
  ....
  factored = whatever;
}

System.out.println(factored);

or have it as an instance variable (meaningless, since it's a local that you need to print but whatever): 或将其作为实例变量(无意义,因为它是您需要打印的局部变量,但无论如何):

class FooBar
{
  String factored;

  void method() {
    ...
    ...
    if (...) {
      ...
      ...
      factored = whatever;
    }

    System.out.println(factored);
  }
}

or thirdly you can return the variable from the method and use it somewhere else: 或者第三,您可以从方法中返回变量,并在其他地方使用它:

class FooBar
{
  String method() {
    ...
    ...
    if (...) {
      ...
      ...
      return whatever;
    }

    return null;
  }

  void otherMethod() {
    String factored = method();
    System.out.println(factored);
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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