简体   繁体   English

“列”无法解析为变量

[英]“Column” cannot be resolved to a variable

i am trying some of the practice New Boston videos and can't get this to work! 我正在尝试一些练习New Boston视频的方法,但无法使它正常工作! I am very new to java, this is my first class. 我对Java非常陌生,这是我的第一堂课。 I have an assignment to do a 2d array and I can't figure out how to get it to display on the screen. 我有一个任务要做一个二维数组,但我不知道如何使它显示在屏幕上。 This practice is from Thenewboston's Java tutorials, video # 34 and it works for him! 这种做法来自Thenewboston的Java教程,视频#34,适用于他!

public class inventory {
public static void main (String[] args) {
    int firstarray[][]= {{8,9,10,11},{12,13,14,15}};
    int secondarray[][]={{30,31,32,33},{43},{4,5,6}};

    System.out.println("This is the first array");              
    display(firstarray);
    System.out.println("This is the second array");
    display(secondarray);
    }


public static void display (int x[][])
{
    for(int row=0;row<x.length;row++)
    {
        for(int column=0;column<x.length;column++);
            System.out.print(x[row][column]+"\t");
        }

     { 

    System.out.println();
     }

} } }}

You've put a ; 你已经放了; after the for loop, negating what you thought was its body. 在for循环之后,否定您所认为的是它的主体。 Get rid of 摆脱

for(int column=0;column<x.length;column++); // <--- this ; 

In this situation, the body of the for loop, where column variable is declared and has scope, is everything after the ) and before the ; 在这种情况下, for循环的主体(声明了column变量并具有作用域)是)之后, ;之前的所有内容; . In other words, nothing. 换句话说,什么都没有。 You actually need to replace the ; 您实际上需要替换; with a { . 带有{


Correct indentation would go a long way in helping you write syntactically correct code. 正确的缩进将有助于您编写语法正确的代码。

You have semicolon at the end of for cycle and not good formating. 您在for循环的结尾使用分号,并且格式不正确。 There are also two brackets, which are absolutely useless :). 还有两个括号,绝对是没用的:)。 The right code can look like this : 正确的代码如下所示:

public static void display(int x[][]) {
    for (int row = 0; row < x.length; row++) {
        for (int column = 0; column < x.length; column++) {
            System.out.print(x[row][column] + "\t");
        }
        System.out.println();
    }
}

Still the display function is not correct, it fails at the end, because the difference of length of rows and columns. 仍然显示功能不正确,由于行和列的长度不同,最后显示失败。

If you want to make this functional, at the second for cycle, you should consider the length of the actual ROW, not how many rows (which is x.length ). 如果要使此功能起作用,请在第二个for周期中考虑实际ROW的长度,而不要考虑多少行(即x.length )。

You only need change column < x.length to column < x[row].length 您只需要将column < x.length更改为column < x[row].length

So the working code is this : 所以工作代码是这样的:

public static void display(int x[][]) {
    for (int row = 0; row < x.length; row++) {
        for (int column = 0; column < x[row].length; column++) {
            System.out.print(x[row][column] + "\t");
        }
        System.out.println();
    }
}

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

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