变量无法在 If 语句中解析 [英] Variable cannot be resolved in an If-Statement

查看:29
本文介绍了变量无法在 If 语句中解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做以下教程问题.

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

所以我有两个问题.

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/教程/java/nutsandbolts/expressions.html

在你的方法 bestCommonFactor 中有三个变量,它们在整个方法中都是有效的: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());

    }

在第一个块中声明的因子不再存在.您可以提取声明并将其放在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆