【问题标题】:Variable "j" cannot be resolved as a variable - Two Dimensional Array变量“j”无法解析为变量 - 二维数组
【发布时间】:2018-07-17 20:53:06
【问题描述】:

我对编程非常陌生(5 天后)。我已经开始使用 Java。 现在,我被这个可怕的错误困住了,我不明白,也不知道如何解决。

public static void main(String[] args) {
    int[][] TwoDim = new int [4][3];  // <-- 1st [rows]   , 2nd [columns]

    // TwoDim[2][1] = 10;  |\|\|\| this way, we can assign number 10 to row 2, column 1 , it's manual this way

    int temp = 10;

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++);
            TwoDim[i][j] = temp; // <<-- why isn't j resolved as a variable?!?!
            temp += 10;
        }
    }
}

我正在创建一个 TwoDim 数组,试图从 youtube 教程中学习一些东西。 eclipse 中的错误说“j”不能作为变量解析,即使我在“for”循环中创建了它。这是否与“i 循环”内的“j 循环”有关?

【问题讨论】:

  • j for 循环声明之后丢失分号;它算作 for 循环的主体!在适当的情况下为该内部 for 循环使用大括号。
  • 您还需要用大括号将要执行的代码包装在 for 循环中。否则只会在循环中执行for循环之后的第一行。

标签: java arrays for-loop compiler-errors


【解决方案1】:

由于for 后面的;,内部循环为空。 j 只定义在那个循环的范围内,它是空的。使用{}为循环打开一个块,你应该没问题:

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) { // No ; here!
        TwoDim[i][j] = temp;
        temp += 10;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 2012-04-14
    • 2021-11-20
    • 1970-01-01
    • 2021-12-21
    • 2012-02-29
    • 2020-12-07
    相关资源
    最近更新 更多