Variable cannot be resolved in an If-Statement

大城市里の小女人 提交于 2021-02-17 03:22:16

问题


I'm trying to do the below tutorial question.

// 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.

And my code is below

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;
    }
}

So I have 2 questions.

1) In my elseif statement, I get an error where factor cannot be resolved to a variable. How do I "carry over" the factor ArrayList from the previous If statement into this elseif statement?

2) I also get a similar error where topnum cannot be resolved. Is this also a placement error for this line of code in my method, or am I making a completely different mistake?


回答1:


Each variable (amongst other 'parts' of java) has a scope, in which it is available. The scope is usually the block in which the variable is declared.

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

In your method greatestCommonFactor there are three variables, which are valid in the whole method: a, b, i

You can access them everywhere from the start of you method (the first open brace) till the end of it (the last close brace).

The block inside your first if statement is an new scope. factor is declared in this scope and is no longer available, after program execution leaves this block.

    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

The else if part is an new block with it's own scope.

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

    }

factor, being declared in the first block does not exists any more. You could pull up the declaration and put it outside the 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;
}



回答2:


1) In my elseif statement, I get an error where factor cannot be resolved to a variable. How do I "carry over" the factor ArrayList from the previous If statement into this elseif statement?

by declaring it before if

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


来源:https://stackoverflow.com/questions/27697049/variable-cannot-be-resolved-in-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!