我尝试过研究,看看能找到什么。我遇到了每个方法的问题。我正在做的是制作一个双数组,并尝试迭代每个数组的每个元素。我一直收到错误*检测到编译错误
行:23 class,interface或enum expected *
我面临的另一个问题是为什么这个方法失败的概念,我的逻辑错在哪里?我很感激你花时间阅读这篇文章,因为我正在努力学习一些基础知识。
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
String[][]nameAdress={{"kn", "vilon"},{"new house","old house"}};
meth(nameAdress);
}
public static void meth(String[][] name){
for(String[] names: name){
for(String field: names){
System.out.println(names);
}
}
}
}
答案 0 :(得分:0)
你混淆了foreach循环头部的顺序
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
String[][]nameAdress={{"karn dhillon", "vinny dhillon"},{"new house","old house"}};
meth(nameAdress);
}
public static void meth(String[][] name){
for(String[] element: name){
for(String eachElement: element){
System.out.println(eachElement);
}
}
}
}
答案 1 :(得分:0)
将您的代码更改为此,现在工作正常
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
String[][]nameAdress={{"karn dhillon", "vinny dhillon"},{"new house","old house"}};
meth(nameAdress);
}
public static void meth(String[][] name){
for(String[] element: name){
for(String field: element){
System.out.println(field);
}
}
}
}