Java 不知道如何构造数组字段';长度是多少?

Java 不知道如何构造数组字段';长度是多少?,java,arrays,Java,Arrays,我不知道怎么做这样的事,以下是我尝试过的: 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, Str

我不知道怎么做这样的事,以下是我尝试过的:

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];
        }
    }
}
公共类法师{
私人int hp;
私人int mp;
私有字符串类型;
私弦薄弱;
private int numSpell;//输入将告诉您数组的长度
公共图像(int-ihp、int-imp、String-itype、String-iweakness、int-inumSpell、String[]ispells){
hp=ihp;
mp=imp;
类型=itype;
软弱=软弱;
私有字符串[]拼写=新字符串[inumSpell];

对于(int i=0;i,尽管您所做的看起来有点奇怪,因为您可以简单地执行ispells.length来获取拼写的数量,而不是为此目的传递另一个int。无论如何,根据您的原始代码,您应该执行以下操作:

(代码的简化版本)


虽然你所做的看起来有点奇怪,因为你可以简单地通过ispells.length来获得法术的数量,而不是通过另一个int来达到这个目的。无论如何,根据你的原始代码,你应该做以下几点:

(代码的简化版本)


在字段中,它看起来像:
private int[]numSpell;
您缺少括号。

在字段中,它看起来像:
private int[]numSpell;
您缺少括号。

是的……它应该可以工作……您甚至试过运行它吗?您不需要
inumSpell
,您可以
ispells.length
来代替。我不相信您真的需要猜测。
拼写的作用是什么?看起来它应该是一个实例变量(
private
access修饰符不允许与局部变量一起使用)。是的……它应该可以工作……你甚至试过运行它吗?你不需要
inumSpell
,你可以
ispells.length
。我不相信你真的需要猜测。
拼写是什么意思?看起来它应该是一个实例变量(
private
access修饰符不允许与局部变量一起使用)非常感谢。对于给我留言的人,我也很感激。Noobs问noob问题,这就是为什么他们被称为Noobs。当我问这个问题时,我很困惑:非常感谢。对于给我留言的人,我也很感激。Noobs问noob问题,这就是为什么他们被称为Noobs。当我问这个问题时,我很困惑问题是:P
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
        }
    }
}