2015-06-22 55 views
0

Eclipse發現了一個缺失的{花括號,但它沒有任何應該出現的地方。具體來說:「令牌上的語法錯誤」;「,{在該令牌之後預計」。意外失蹤{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 

我的最後一行代碼也有一個缺失的大括號}。

有什麼不對?

+0

提醒一句:當你會得到關於你不明白的語法的錯誤,你可能會犯這樣的混亂,以至於編譯器不能給你一個相關的錯誤。這是教你的第一個時刻:你所看到的問題通常只是另一個問題的結果;縮小並查看更大的圖片。 – Gimby

+0

謝謝金比!是的,我認爲這是更普遍的錯誤,但我不知道你不能在類中生成可執行語句。另一方面,我並不真正瞭解downvote;如果這是合理的,我會好奇爲什麼。這似乎是一個公平的問題,答案有9個upvotes,所以它幫助了很多人。 – doublefelix

回答

9

在Java中,你不能直接寫在class.So可執行語句,這是錯誤的語法:

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

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

可執行語句只能在方法/構造函數/代碼塊