我想显示多个数组。
public class apples {
public static void main(String[] args){
int Tabela_Nr1[][] = {{8,9,10,11}, {12,13,14,15}};
int Tabela_Nr2[][] = {{30,31,32,33}, {43},{4,5,6,}};
System.out.println("To jest pierwsza tabela: ");
wyswietl(Tabela_Nr1);
System.out.println("To jest druga tabela: ");
wyswietl(Tabela_Nr2);
}
public static void wyswietl(int x [][]){
for (int row = 0; row < x.length; row++){
for(int counter = 0; counter < x[row].length; counter++);
System.out.print(x[row][counter] + "\t");
}
System.out.println();
}
}
调试器告诉我变量计数器没有声明,但它在&#34;对于&#34;环... 问题是什么?
控制台:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
counter cannot be resolved to a variable
at apples.wyswietl(apples.java:15)
at apples.main(apples.java:8)
答案 0 :(得分:4)
问题是额外的半结肠:
for (int row=0;row<x.length;row++){
for(int counter=0;counter<x[row].length;counter++); //<-- here
System.out.print(x[row][counter]+"\t");
这个半冒号结束了内部循环,这意味着print语句在循环之外,counter
未知。
我认为您的System.out.println();
也被错误地放置了。您可能希望在每行之后放置它:
public static void wyswietl(int x[][]){
for (int row=0;row<x.length;row++) {
for(int counter=0;counter<x[row].length;counter++) {
System.out.print(x[row][counter]+"\t");
}
System.out.println();
}
}
答案 1 :(得分:2)
for(int counter=0;counter<x[row].length;counter++);
- &gt;删除;
,它会正常工作:)
Arrays.deepToString()
显示多维数组。