public class Weapons
{
private String[] weaponType;
public Weapons()
{
this.weaponType[] = {"Melee", "Range", "Magic"};
}
}
显示2个错误
语法错误,插入";完成BlockStatements
语法错误,插入"AssignmentOperator Expression"完成作业
如何从数组的实例变量中正确地分配数组内的元素?
this.weaponType[] = {"Melee", "Range", "Magic"};
应该是
this.weaponType = new String [] {"Melee", "Range", "Magic"};
您在赋值时遗漏了类型,您不需要在赋值时再次添加这些[]
。
如果你想把赋值移到top,因为有静态数据,你可以简单地写
private String[] weaponType ={ "Melee", "Range", "Magic" };