2017-08-03 68 views
0

为什么我不能传递数组?为什么我不能将这个数组传递给我的目标类?

for (int a = 0;; a++) { 
     System.out.print("Enter Name:"); 
     name[a] = in.nextLine(); 
     System.out.print("Enter Age:"); 
     age[a] = in.nextInt(); 
     in.nextLine(); 
     Student student = new Student(name[], age[]); 
    } 

的错误说,它需要在阵列后的.class 帮助

+6

简单的拼写错误 - '学生学生=新的学生(姓名,年龄);' –

+2

^,并宣布你的数组是'String [] name'而不是'String name []'。它的可读性更高。 –

回答

1

取决于你的构造函数(即它所接受)学生类的,如果它接受阵列

然后

学生=新生(姓名,年龄); //在这种情况下,这行应该是后的for循环

它是否接受单个名称和阵列

然后

学生的学生=新学生(名称并[a],年龄[A]);

0

为什么我不能传递我的数组?

,因为你的语法不正确,这在这里:

Student student = new Student(name[], age[]); 

的原因,以便如果您引用数组的形式传递一个数组你通常做

Student student = new Student(name, age); 

的参数,那么你可以:

申报方法

void doSomeTricks(String[] args) { 

但调用/调用你做

doSomeTricks(myArray); 

和方法不

doSomeTricks(myArray[]); 
相关问题