java---for循环

时间:2021-02-27 13:41:34   收藏:0   阅读:0

for(初始化; 布尔表达式; 更新) {
//代码语句
}



package hello.demo;

public class hello2 {
public static void main(String[] args){
for (int x = 20; x < 50; x++){
System.out.println("the value of x is "+x);
System.out.println("update x");
}
}
}

增强型的java for循环

for(声明语句 : 表达式) {
//代码句子
}

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

 

可以把增强型的for语句理解为python中的for i in list ,用于循环list中的元素



package hello.demo;

public class hello2 {
public static void main(String[] args){
String[] StringList={"guoshun1","guoshun2","guoshun3"};
for (String i : StringList){
System.out.println("The Stirng in StringList is "+i);
}
int[] IntList={1,2,3,33,44,55};
for (int j : IntList){
System.out.println("the int in Intlist is "+j);
}
}
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!