';'预期和非法的表达错误开始

时间:2013-08-05 22:11:12

标签: java compiler-errors

我是linux中的java编程但它报告了以下错误。有人可以帮我吗?在期待中感谢!

Break.java:4: ';' expected  
    for (int x:numbers) {  
              ^  
Break.java:11: illegal start of expression  
  }  
  ^  
2 errors

public class Break {
    public static void main(String args[]) {
        int [] numbers={10,20,30,40,50};
        for (int x:numbers){
            if (x==30){
                break;
            }
            System.out.print(x);
            System.out.print("\n");
        }
    }
}

1 个答案:

答案 0 :(得分:9)

您必须使用JDK 1.4或更早版本。您的代码编译为1.5,但不是1.4:

$ javac Break.java
$ javac -source 1.4 -target 1.4 Break.java
Break.java:4: for-each loops are not supported in -source 1.4
(try -source 1.5 to enable for-each loops)
    for (int x:numbers){
              ^
1 error

您必须使用Java 1.5+来使用foreach循环语法which was introduced in Java 1.5