2017-08-03 64 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[]); 
相關問題