Eclipse 'Syntax error on token ",", Expression expected after this token',我的语法哪里错了?
Eclipse 'Syntax error on token ",", Expression expected after this token', where is my syntax wrong?
package MainFunction;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gun m_one = new Gun("M1", 30, ["Bleeding"], "semi-auto", ".15 cal", "lead", 8);
System.out.println("Java Works.");
}
}
m_one 中存在语法错误。它说:
标记“,”的语法错误,此标记后应有表达式
我不明白这是从哪里来的。这是我的构造函数:
package MainFunction;
public class Weapon {
Weapon(String name, int damage, String[] effects){
this.name = name;
this.damage = damage;
this.effects = effects;
}
private int damage;
public String name;
private String[] effects;
public void addEffect(String effect){
effects[effects.length] = effect;
}
}
class Gun extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private String ammoType;
private String bulletMaterial;
private int barrelLength;
private String fireType;
Gun( String name, int damage, String[] effects, String fireType, String ammoType, String bulletMaterial, int barrelLength) {
super(name, damage, effects);
// TODO Auto-generated constructor stub
this.name = name;
this.damage = damage;
this.effects = effects;
this.fireType = fireType;
this.ammoType = ammoType;
this.type = "ranged";
this.bulletMaterial = bulletMaterial;
this.barrelLength = barrelLength;
}
}
class Sword extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private int bladeLength;
private int hiltLength;
private String bladeMaterial;
Sword(String name, int damage, String[] effects, int barrelLength, int hiltLength, String bladeMaterial){
super(name, damage, effects);
this.name = name;
this.damage = damage;
this.effects = effects;
this.type = "melee";
this.bladeLength = bladeLength;
this.hiltLength = hiltLength;
this.bladeMaterial = bladeMaterial;
}
}
构造函数returns 没有错误,但是当我使用它时却出错了。有什么问题?
您需要在 m_one
的声明中使用大括号而不是方括号,围绕 "Bleeding"。
您正在尝试使用以下表达式创建和传递字符串数组:
["Bleeding"]
但是,这不是用于创建数组的 Java 语法。您必须显式创建数组,指定类型,以及包含内容的大括号 {}
。
new String[] {"Bleeding"}
如果您愿意,您可以拥有多个元素:
new String[] {"Bleeding", "Damaging"};
此外,稍后在 Weapon
的 addEffect
方法中,您尝试通过分配超出数组长度的元素来增加数组长度。
effects[effects.length] = effect;
在 Java 中,数组一旦创建就具有固定大小,因此这将引发 ArrayIndexOutOfBoundsException
。如果您需要可变长度数组,请将 String[]
替换为 List<String>
。首先,在构造函数中:
Weapon(String name, int damage, List<String> effects){
然后在声明时 effects
:
List<String> effects;
然后在定义addEffect
时:
effects.add(effect);
无需在 main
中创建数组,您需要创建 List
的实现并将其传入。
List<String> effects = new ArrayList<>();
effects.add("Bleeding");
effects.add("Damaging");
Gun m_one = new Gun("M1", 30, effects, "semi-auto", ".15 cal", "lead", 8);
package MainFunction;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gun m_one = new Gun("M1", 30, ["Bleeding"], "semi-auto", ".15 cal", "lead", 8);
System.out.println("Java Works.");
}
}
m_one 中存在语法错误。它说: 标记“,”的语法错误,此标记后应有表达式
我不明白这是从哪里来的。这是我的构造函数:
package MainFunction;
public class Weapon {
Weapon(String name, int damage, String[] effects){
this.name = name;
this.damage = damage;
this.effects = effects;
}
private int damage;
public String name;
private String[] effects;
public void addEffect(String effect){
effects[effects.length] = effect;
}
}
class Gun extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private String ammoType;
private String bulletMaterial;
private int barrelLength;
private String fireType;
Gun( String name, int damage, String[] effects, String fireType, String ammoType, String bulletMaterial, int barrelLength) {
super(name, damage, effects);
// TODO Auto-generated constructor stub
this.name = name;
this.damage = damage;
this.effects = effects;
this.fireType = fireType;
this.ammoType = ammoType;
this.type = "ranged";
this.bulletMaterial = bulletMaterial;
this.barrelLength = barrelLength;
}
}
class Sword extends Weapon {
private String type;
public String name;
private int damage;
public String[] effects;
private int bladeLength;
private int hiltLength;
private String bladeMaterial;
Sword(String name, int damage, String[] effects, int barrelLength, int hiltLength, String bladeMaterial){
super(name, damage, effects);
this.name = name;
this.damage = damage;
this.effects = effects;
this.type = "melee";
this.bladeLength = bladeLength;
this.hiltLength = hiltLength;
this.bladeMaterial = bladeMaterial;
}
}
构造函数returns 没有错误,但是当我使用它时却出错了。有什么问题?
您需要在 m_one
的声明中使用大括号而不是方括号,围绕 "Bleeding"。
您正在尝试使用以下表达式创建和传递字符串数组:
["Bleeding"]
但是,这不是用于创建数组的 Java 语法。您必须显式创建数组,指定类型,以及包含内容的大括号 {}
。
new String[] {"Bleeding"}
如果您愿意,您可以拥有多个元素:
new String[] {"Bleeding", "Damaging"};
此外,稍后在 Weapon
的 addEffect
方法中,您尝试通过分配超出数组长度的元素来增加数组长度。
effects[effects.length] = effect;
在 Java 中,数组一旦创建就具有固定大小,因此这将引发 ArrayIndexOutOfBoundsException
。如果您需要可变长度数组,请将 String[]
替换为 List<String>
。首先,在构造函数中:
Weapon(String name, int damage, List<String> effects){
然后在声明时 effects
:
List<String> effects;
然后在定义addEffect
时:
effects.add(effect);
无需在 main
中创建数组,您需要创建 List
的实现并将其传入。
List<String> effects = new ArrayList<>();
effects.add("Bleeding");
effects.add("Damaging");
Gun m_one = new Gun("M1", 30, effects, "semi-auto", ".15 cal", "lead", 8);