gpt4 book ai didi

java - Eclipse 中意外缺少 { 大括号

转载 作者:行者123 更新时间:2023-12-01 06:50:34 25 4
gpt4 key购买 nike

Eclipse 发现缺少 { 大括号,但它不应该在任何地方。具体来说:“标记“;”上的语法错误,{预期在此标记之后”。

要么是我错过了一些愚蠢的东西,要么是我必须学习一些关于 Java 的新东西。这是有问题的类:

class usedCars{
Random r = new Random();

String[] allColors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"};
String[] allMakes = {"honda", "mercedes", "rolls-royce", "audi"};

String[] colorList = new String[10];
String[] makeList = new String[10];
int[] yearList = new int[10];
int[] speedList = new int[10]; //here is the error mentioned above

//fill the lists
for(int i=0; i<10; i++) {

this.colorList[i] = this.allColors[this.r.nextInt(this.allColors.length)]; }
} //usedCars class

我的最后一行代码也缺少右大括号}。

出了什么问题?

最佳答案

在Java中,你不能直接在类中编写可执行语句。所以这在语法上是错误的:

for(int i=0; i<10; i++) { 

this.colorList[i] = this.allColors[this.r.nextInt(this.allColors.length)];
}

可执行语句只能在方法/构造函数/代码块中

关于java - Eclipse 中意外缺少 { 大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com