在打印多维数组元素时出现错误(Java)

huangapple go评论58阅读模式
英文:

Error in printing elements of an Multidimensional Array (Java)

问题

我想打印出矩阵的所有元素但是编译器报错"找不到符号"我想知道确切的问题及其解决方法

class Main{
    public static void main(String[] args){
        int[][] matrix = {
            {3, 2, 1},
            {3, 2, 1},
            {5, 5, 8}
    };
    
    for (int[] i : matrix)
        for (int j : i)
            System.out.println("在第" + i + "行,第" + j + "列的元素为" + matrix[i][j]);
    }
}
英文:

I want to print all the elements of the matrix. But the complier throws "Cannot find symbol" error. I want to know the exact problem and its remedy.

class Main{
    public static void main(String[] args){
        int[][] matrix = {
            {3, 2, 1},
            {3, 2, 1},
            {5, 5, 8}
    };
    
    for (int[] i : matrix);
        for ( int j : i);
    System.out.println("At Row "+ i + " at column" + j + " = " + matrix[i][j]);
}
}

答案1

得分: 2

你有两个问题:

  1. 你需要迭代索引,而不是对象。

  2. 在应该有一个开括号(“{”)的地方,你有一个“;”。

        for (int i = 0; i < matrix.length; i += 1) {
            for (int j = 0; j < matrix[i].length; j += 1) {
                System.out.println("在第 " + i + " 行,第 " + j + " 列 = " + matrix[i][j]);
            }
        }
在第 0 行,第 0 列 = 3
在第 0 行,第 1 列 = 2
在第 0 行,第 2 列 = 1
在第 1 行,第 0 列 = 3
在第 1 行,第 1 列 = 2
在第 1 行,第 2 列 = 1
在第 2 行,第 0 列 = 5
在第 2 行,第 1 列 = 5
在第 2 行,第 2 列 = 8
英文:

You have two issues:

  1. You need to iterate over the indexes and not the objects

  2. You have a ";" where you should have an opening brace ("{")

        for (int i = 0; i &lt; matrix.length; i += 1) {
            for (int j = 0; j &lt; matrix[i].length; j += 1) {
                System.out.println(&quot;At Row &quot;+ i + &quot; at column&quot; + j + &quot; = &quot; + matrix[i][j]);
            }
        }
At Row 0 at column0 = 3                                                         
At Row 0 at column1 = 2                                                         
At Row 0 at column2 = 1                                                         
At Row 1 at column0 = 3                                                         
At Row 1 at column1 = 2                                                         
At Row 1 at column2 = 1                                                         
At Row 2 at column0 = 5                                                         
At Row 2 at column1 = 5                                                         
At Row 2 at column2 = 8

答案2

得分: 2

Allen已经指出了你的错误。
如果你想用foreach循环实现相同的功能:

public static void main(String[] args) {
    int[][] matrix = { { 3, 2, 1 }, { 3, 2, 1 }, { 5, 5, 8 } };

    for (int[] i : matrix) {
        System.out.print("行 --> ");
        for (int j : i) {
            System.out.print(j + " ");
        }
        System.out.println();
    }
}
英文:

Allen already point out your mistakes.
If you want to achieve the same with foreach loop :

public static void main(String[] args) {
		int[][] matrix = { { 3, 2, 1 }, { 3, 2, 1 }, { 5, 5, 8 } };

		for (int[] i : matrix) { 
			System.out.print(&quot;Row --&gt; &quot;);
			for (int j : i) {
				System.out.print(j + &quot; &quot;);
			}
			System.out.println();
		}
	}

huangapple
  • 本文由 发表于 2020年9月7日 11:39:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定