简体   繁体   English

为什么我的数组“不能解析为变量”?

[英]Why can my array "Not be resolved to a variable"?

static void createArray(String function) {

 int j = 10;
 int results[]; 
 results = new int[j+1];

for (int i = 0; i < j; i++) {

    String stringI = Integer.toString(i);
    int sum = 0;
    function = function.replace("X",stringI);
    String[] Pluses = function.split("\\+");

    for (String multString: Pluses) {

    String[] mults = multString.split("\\*");
        int multAcc = 1;
    for (String num: mults)
        {
        multAcc *= Integer.parseInt(num);
        }
        sum = sum + multAcc;

        }
        results[i] = sum;   
    }
}

public static void main (String args[]) {

Hello, exscuse me for bad formatting, this is my first post on Stackoverflow.你好,请原谅我格式错误,这是我在 Stackoverflow 上的第一篇文章。

When compiling I encounter an error that says that "results cannot be resolved to a variable".编译时我遇到一个错误,提示“结果无法解析为变量”。 The code is supposed to create an array containing the integer values of the first 10 values of a function that i've written into the console.该代码应该创建一个数组,其中包含我写入控制台的函数的前 10 个值的整数值。

    Scanner myScanner = new Scanner(System.in);
    System.out.println("Enter function");

    String function = myScanner.nextLine();
    createArray(function);
    System.out.println(Arrays.ToString(results));

    myScanner.close();
    }

Local variables inside a { ... } are not accessible outside. { ... }内部的局部变量在外部无法访问。

static int[] createArray(String function) {
    int j = 10;
    int[] results = new int[j+1];

    for (int i = 0; i < j; i++) {
        String stringI = Integer.toString(i);
        int sum = 0;
        function = function.replace("X", stringI);
        String[] pluses = function.split("\\+");

        for (String multString: pluses) {

            String[] mults = multString.split("\\*");
            int multAcc = 1;
            for (String num: mults)
            {
                multAcc *= Integer.parseInt(num);
            }
            sum += multAcc;

        }
        results[i] = sum;
    }
    return results;
}

    int[] array = createArray(function);
    System.out.println(Arrays.toString(array));

Let the function return the array, and use a new variable at the call.让函数返回数组,并在调用时使用一个新变量。

The above probably will not do what you intended:以上可能不会做你想要的:

  • replace replaces all occurrences of the letter X. A second variable? replace替换所有出现的字母 X。第二个变量?

The Joop Eggen's Answer is correct. Joop Eggen 的回答是正确的。 But I think that you must to checkout this link Scope of the variables in Java但我认为你必须检查这个链接Java中变量的范围

results is local variable inside createArray() method.结果createArray()方法中的局部变量。 In order to solve this issue you can do either of following:为了解决此问题,您可以执行以下任一操作:

  1. Declare results as static member in class在类中将结果声明为静态成员
  2. Return array of integer from createArray() method.createArray()方法返回整数数组。

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

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