我用eclipse时报错说不是java se 5.0标准的

来源:百度知道 编辑:UC知道 时间:2024/09/20 22:48:51
public class text5{
text5(String[] args){
for(String s : args)
System.out.println(s);
}
public static void main(String[] args){
String[] s = {"h","e","l","l","o"};
text5 t = new text5(s);

}
}
编译时报错:
Syntax error, 'for each' statements are only available if source level is 5.0

这跟eclipse无关,跟你的jdk版本有关。
你写的for循环用了“foreach”语法,jdk版本5以下不支持“foreach”,5.0或以上才支持(你把那句报错的意思理解错了),是从C#学过来的。
升级你的jdk就能解决这个问题,或者你可以用以下代码代替你现在的代码
public class text5{
text5(String[] args){
for(int i=0;i<args.length();i++)
{
String s=args[i];
System.out.println(s);
}
public static void main(String[] args){
String[] s = {"h","e","l","l","o"};
text5 t = new text5(s);
}
}
最新的jdk版本是6.0,下载地址
http://192.18.108.239/ECom/EComTicketServlet/BEGIN31DA9601C7CEF4A95D66372DE469CD8C/-2147483648/2617791231/1/877946/877778/2617791231/2ts+/westCoastFSEND/jdk-6u5-oth-JPR/jdk-6u5-oth-JPR:2/jdk-6u5-windows-i586-p.e