我仍然在學習使用方法,但是我在路上又碰到了另一個殘端。我試圖在另一個靜態無效方法中調用一個靜態void方法。所以它大致看起來像這樣:調用靜態無效方法
public static void main(String[] args) {
....
}
//Method for total amount of people on earth
//Must call plusPeople in order to add to total amount of people
public static void thePeople (int[][] earth) {
How to call plusPeople?
}
//Method adds people to earth
//newPerson parameter value predefined in another class.
public static void plusPeople (int[][] earth, int newPerson) {
earth = [newPerson][newPerson]
}
我已經嘗試了幾個不同的東西,但沒有真正奏效。
int n = plusPeople(earth, newPerson);
//Though I learned newPerson isn't recognized
because it is in a different method.
int n = plusPeople(earth); \
//I don't really understand what the error is saying,
but I'm guessing it has to do with the comparison of these things..[]
int n = plusPeople;
//It doesn't recognize plusPeople as a method at all.
我覺得不能夠,甚至調用一個方法很愚蠢,但我硬是被卡在這個問題上約兩個小時了。
我真的不確定如何調用除了使用int'變量'以外的方法。我的教授只告訴我們如何調用使用int和double的方法。所以她也得到了返回值。有沒有一種方法可以用它來代替int n? – Sozziko
你混淆了參數和返回值。方法參數的類型和數量與返回的內容沒有任何關係。你可以有'int foo()'和'void bar(int i)'。第一個不接受任何參數並返回一個int。第二個接受int作爲參數,並且不返回任何內容。 –