我正在为一些随机竞赛的参赛者编写一个简单的 Java 代码。我添加了年龄、类别和分数等变量。如果我添加整数或字符串,没有问题,但是当想要添加 int 数组(它在实例化它并写入构造函数时起作用)作为类的参数时,它不起作用并将数组视为 5 个离散的整数。我究竟做错了什么?private int [] danceScores = new int[5]; //instance variablepublic Competitor(int CNumber, String cName, String cLevel, String dStyle, int[] cScores){ ...; danceScores = cScores;}// in Main methodCompetitor competitor1= new Competitor(1, "name", "level", "dance", {1,2,3,4,5});
3 回答
ITMISS
TA贡献1871条经验 获得超8个赞
传递的整数数组未格式化。这里语法错误。
应该像下面这样
Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[] {1,2,3,4,5});
缥缈止盈
TA贡献2041条经验 获得超4个赞
上面的构造函数调用应该给出编译错误,指出“此处不允许数组初始值设定项”。添加new int[]
Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[] {1,2,3,4,5});
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
你必须像这样通过它
Competitor competitor1= new Competitor(1, "name", "level", "dance", new int[]{1,2,3,4,5});
添加回答
举报
0/150
提交
取消