无法在If语句中解析变量

时间:2014-12-29 22:38:08

标签: java if-statement arraylist

我正在尝试执行以下教程问题。

// Create a method called greatestCommonFactor
// It should return the greatest common factor
// between two numbers.  
//
// Examples of greatestCommonFactor:
//   greatestCommonFactor(6, 4)   // returns 2
//   greatestCommonFactor(7, 9)   // returns 1
//   greatestCommonFactor(20, 30) // returns 10
//
// Hint: start a counter from 1 and try to divide both
// numbers by the counter. If the remainder of both divisions
// is 0, then the counter is a common factor. Continue incrementing
// the counter to find the greatest common factor. Use a while loop
// to increment the counter.

我的代码在

之下
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Ex4_GreatestCommonFactor {

    // This is the main method that is executed as
    // soon as the program starts.  
    public static void main(String[] args) {
        // Call the greatestCommonFactor method a few times and print the results
    }

    public static int greatestCommonFactor(int a, int b){
        int i = 1 ;
        while (i >= 1 ){
            i++;
        }
        if (a%i == 0 && b%i == 0){
            ArrayList factor = new ArrayList<Integer>();
            factor.add(i);  
        }
        else if (a%i <= 1 || b%i <= 1){
            Collections.sort(factor);
            List<Integer> topnum = factor.subList(factor.size() - 1, factor.size());

        }
        return topnum;
    }
}

所以我有2个问题。

1)在我的elseif语句中,我收到一个错误,其中factor无法解析为变量。我如何&#34;结转&#34;从前一个If语句中的factor ArrayList到这个elseif语句?

2)我也遇到类似的错误,topnum无法解决。这也是我方法中这行代码的放置错误,还是我犯了一个完全不同的错误?

2 个答案:

答案 0 :(得分:3)

  

1)在我的elseif语句中,我得到一个错误,其中因子无法解析为变量。如何将上一个If语句中的因子ArrayList“转移”到此elseif语句?

之前通过声明

ArrayList factor = null;
if ( /* some condition */ ) {
   // initialize factor
} else if (/* some condition */) {
  // access factor here
}

答案 1 :(得分:1)

每个变量(以及java的其他部分)都有一个范围,在该范围内可用。 范围通常是声明变量的块。

阻止:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

在你的方法greatCommonFactor中有三个变量,它们在整个方法中有效: a,b,i

您可以从方法的开头(第一个打开的大括号)到它的结尾(最后一个大括号)随处访问它们。

第一个if语句中的块是一个新范围。在程序执行离开此块后,factor在此作用域中声明并且不再可用。

    if (a%i == 0 && b%i == 0){                       // Start of Block/Scope
        ArrayList factor = new ArrayList<Integer>(); // Declaration, you can access factor now
        factor.add(i);  
    }                                                // End of Block/Scope, factor inaccessible

else if if部分是一个新的块,它有自己的范围。

    else if (a%i <= 1 || b%i <= 1){
        Collections.sort(factor);
        List<Integer> topnum = factor.subList(factor.size() - 1, factor.size());

    }

因子,在第一个块中声明不再存在。 您可以提取声明并将其放在if。

之外
public static int greatestCommonFactor(int a, int b){
    int i = 1 ;
    while (i >= 1 ){
        i++;
    }
    ArrayList<Integer> factor = new ArrayList<Integer>();
    if (a%i == 0 && b%i == 0){
        factor.add(i);  
    }
    else if (a%i <= 1 || b%i <= 1){
        Collections.sort(factor);
        List<Integer> topnum = factor.subList(factor.size() - 1, factor.size());

    }
    return topnum;
}
相关问题