1

私はまだメソッドの使い方を学んでいますが、途中で別の切り株にぶつかりました。別の static void メソッドで static 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.

メソッドを呼び出すことさえできないのは非常にばかげていると思いますが、文字通りこの問題で約2時間立ち往生しています.

4

2 に答える 2

3

If it's void, you can't assign it to anything.

Just call using

int n = 5;
plusPeople(earth, n);

The first error you're getting is because the newPerson isn't defined locally (you're right, it's in a different method) The second error you're getting is because the return type "void" was not an "int". The third error you're getting is because it has no parenthesis, and probably thinks there should be a variable named plusPeople.

于 2012-10-12T22:06:04.430 に答える
2

2つの引数を指定する必要があります。最初のものはタイプint[][](およびearth修飾)である必要があり、2番目のものはintである必要があります。したがって、たとえば:

plusPeople(earth, 27);

もちろん、それは技術的な答えにすぎません。引数として実際に渡す必要があるものは、メソッドが引数をどのように処理するか(javadocで指定する必要があります)、引数が何を意味するか(javadocで指定する必要があります)、およびメソッドに何をさせたいかによって異なります。 (知っておくべきこと)。

また、メソッドはとして宣言されているため、void plusPeople(...)何も返さないことに注意してください。したがって、そうint n = plusPeople(earth, newPerson);することは意味がありません。

于 2012-10-12T22:07:14.483 に答える