0

そのようなことを行う方法がわかりません。私が試したことは次のとおりです。

public class Mage{
    private int hp;
    private int mp;
    private String type;
    private String weakness;
    private int numSpell;//the input will tell you how long the array will be
    public Mage(int ihp, int imp, String itype, String iweakness, int inumSpell, String[] ispells){
        hp=ihp;
        mp=imp;
        type=itype;
        weakness=iweakness;
        private String[] spells = new String[inumSpell];
        for(int i=0;i<ispells.length;i++){
            spells[i]=ispells[i];
        }
    }
}

私の推測は正しいと思いますか?どんな助けでも大歓迎です、ありがとう。

4

2 に答える 2

3

あなたがしていることは少し奇妙に見えますが、そのような目的のために別の int を渡す代わりに、単純に ispells.length を実行して呪文の数を取得できます。とにかく、元のコードに基づいて行うべきことは次のとおりです。

(コードの簡易版)

public class Mage {
    private String[] spells;

    public Mage(int noOfSpells, String[] ispells) {
        spells = new String[noOfSpells];
        for (......) {
             // your for loop to copy from ispells to spells
        }
    }
}
于 2012-11-28T04:26:39.977 に答える
0

フィールドでは、次のようprivate int[] numSpell;になります。括弧がありません。

于 2012-11-28T04:33:24.340 に答える