java的for循环和c++的for循环类似
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
运行输出:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
Java 引入了一种主要用于数组的增强型 for 循环。
public class Puppy{
public static void main(String[] args){
int[] Nums = {10,20,30,40,50,60};
for(int x : Nums){
System.out.print(x);
System.out.print(",");
} System.out.println();
String[] Names = {"Jacky","Tom","Lily"};
for(String name : Names){
System.out.print(name);
System.out.print(',');
}
}
}
运行输出:
10,20,30,40,50,60,
Jacky,Tom,Lily,