1. for(int name:nameArray){}
2. for(){}
3. while{}
4. do{}while{}
public class Loop{
public static void main(String[] args){
int[] numList = {1,2,3,4,5,6,7,8,9,10};
//1.打印数组numList所有值
for(int i : numList){
System.out.print(i + " ");
}
System.out.println();
//2.打印数组numList的偶数
for(int i=0;i<numList.length;i++){
if(numList[i] % 2 ==0)
System.out.print(numList[i] + " ");
}
System.out.println();
//3.打印数组numList的奇数
int i = 0;
while(i < numList.length){
if(numList[i] % 2 != 0){
System.out.print(numList[i] + " ");
}
i++;
}
System.out.println();
//4.累加
i = 0;
int sum = 0;
do{
sum += numList[i];
i++;
} while(i < numList.length);
System.out.print(sum);
}
}
运行结果如下:
E:\java>javac Loop.java
E:\java>java Loop
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10
1 3 5 7 9
55