Java 语法错误,“for each”语句仅在源级别为 1.5 或更高时可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21356676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error, 'for each' statements are only available if source level is 1.5 or greater
提问by user3220565
Receiving the below error:
收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, 'for each' statements are only available if source level is 1.5 or greater at Tuto.OneDimArrays.main(OneDimArrays.java:14)
CODE:
代码:
public class OneDimArrays {
public static void main(String[] args) {
int[] xabc = new int[5];
xabc[2]=20;
xabc[0]=50;
for(int temp:xabc){
System.out.println(temp);
}
}
}
Please help..
请帮忙..
回答by ApproachingDarknessFish
The error tells you exactly what the problem is. You are using a for-each loop:
该错误会准确地告诉您问题是什么。您正在使用 for-each 循环:
for(int temp : xabc)
{
System.out.println(temp);
}
This syntax was only added in Java 1.5, so you appear to be compiling using an earlier version. Without knowing what IDE/environment you're using, I can't tell you how to fix this, but see if you can find a way to compile your code using a more up-to-date version of java.
此语法仅在 Java 1.5 中添加,因此您似乎在使用早期版本进行编译。在不知道您使用的是什么 IDE/环境的情况下,我无法告诉您如何解决这个问题,但是看看您是否可以找到一种使用更新版本的 java 编译代码的方法。
Note that there is a difference between the JRE (Java Runtime Environment) and the JDK (Java Development Kit). The JRE is used to run java programs on your computer, while the JDK is used to write java programs on your computer. Normally your system only warns you when the JRE is outdated (as most computer users don't write code and probably don't even have the JDK installed). Therefore, even if your JRE is up to date, you won't be able to compile the latest features unless you have the right JDK.
请注意,JRE(Java 运行时环境)和 JDK(Java 开发工具包)之间存在差异。JRE用于在您的计算机上运行java程序,而JDK用于在您的计算机上编写java程序。通常,您的系统只会在 JRE 过时时向您发出警告(因为大多数计算机用户不会编写代码,甚至可能没有安装 JDK)。因此,即使您的 JRE 是最新的,除非您拥有正确的 JDK,否则您将无法编译最新的功能。
If you're using an old JDK, you can download the latest version here.
如果您使用的是旧版 JDK,则可以在此处下载最新版本。
If you're using an up-to-date JDK, you will have to change some settings in your project in order to use it.
如果您使用的是最新的 JDK,则必须更改项目中的某些设置才能使用它。
If for whatever reason neither of these are an option, you can emulate the for-each loop using pre-1.5 language constructs. The most basic approach is described in Pat's answer.
如果由于某种原因这些都不是一个选项,您可以使用 1.5 之前的语言结构模拟 for-each 循环。Pat的回答中描述了最基本的方法。
回答by Pat
Compile this in a later version of java if you can.
如果可以,请在更高版本的 java 中编译它。
If you are unable to use a later version of java for some reason just use a regular for loop:
如果由于某种原因无法使用更高版本的 java,只需使用常规 for 循环:
for(int i = 0; i < xabc.length; i++) {
int temp = xabc[i];
System.out.println(temp);
}
回答by Waqas Khalid Obeidy
If you are using Eclipse IDE then right click on the project, goto properties, Java Compiler, Check Enable project specific settings and set the Compiler compliance level to greater than 1.5. I solved this issue by setting it to 1.6. Hope it helps
如果您使用的是 Eclipse IDE,则右键单击该项目,转到属性、Java 编译器、选中启用项目特定设置并将编译器合规性级别设置为大于 1.5。我通过将其设置为 1.6 解决了这个问题。希望能帮助到你
回答by Aman Chawla
Try changing compiler compilance level in eclipse:-
尝试在 Eclipse 中更改编译器的合规性级别:-
- click Windows , go to preferences.
- select java from the options ,select compiler in drop down menu.
- change compiler compilance level to a value 1.5 or greater.
- 单击 Windows ,转到首选项。
- 从选项中选择 java,在下拉菜单中选择编译器。
- 将编译器合规性级别更改为 1.5 或更高的值。
click on the link for reference: https://i.stack.imgur.com/79tvV.png
点击链接参考:https: //i.stack.imgur.com/79tvV.png
回答by Dipan Mitra
This is for eclipse.
这是为了日食。
window -> preferences -> java -> compiler -> configure project specific settings -> double click on created 'project name' -> uncheck 'use compliance from execution environment' -> then select 'compiler compliance level' to 1.5 or higher(best to choose the last level which is maximum and latest)
窗口 -> 首选项 -> java -> 编译器 -> 配置项目特定设置 -> 双击创建的“项目名称” -> 取消选中“使用执行环境合规性” -> 然后选择“编译器合规性级别”为 1.5 或更高(最好选择最大和最新的最后一个级别)
Done. now apply and close all. re run the program.
完毕。现在申请并关闭所有。重新运行程序。