简体   繁体   中英

Enhance loop in java not working in IDE. how to make it work?

I am currently learning java in Eclipse IDE. and I used enhanced loop in a simple program and it is showing error. here is the code

public final class enhancedFor {
    public static void main(String args[]){
        int jatin[]={4,5,6,7,8};
        int total=0;
        for (int x: jatin){
            total+=x;

        }
        System.out.println(total);

    }

}

It is showing error where I have declared the for loop. " for each is only available if source level is 1.5 or greater."

Go to project properties and correct the compiler version:

在此处输入图片说明

You may need to upgrade your Java too.

int[] jatin = {4,5,6,7,8};

for (int x=0;x<jatin.length;x++) {
    total+=jatin[x];        
}

this would fix your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM