变量无法在If语句中解析 [英] Variable cannot be resolved in an If-Statement
问题描述
我正在尝试下面的教程问题.
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.
我的代码在下面
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
无法解析为变量.如何将前一个If语句的 factor
ArrayList继承到此elseif语句中?
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)我也遇到类似的错误,其中 topnum
无法解析.这也是我方法中这一行代码的放置错误,还是我犯了一个完全不同的错误?
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?
推荐答案
每个变量(在Java的其他部分"中)都有一个范围,在该范围内可用.作用域通常是在其中声明变量的块.
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.
块: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
在您的方法greatCommonFactor中,存在三个变量,它们在整个方法中均有效:a,b,i
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).
第一个if语句中的块是新作用域.因子在此范围内声明,并且在程序执行离开此块后不再可用.
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
else if部分是具有自己作用域的新块.
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不再存在.您可以拉起声明并将其放在if之外.
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;
}
这篇关于变量无法在If语句中解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!