package com.lemon.operator;
public class for_demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// for循环,格式
for(int i = 1;i<=4;i++) {
System.out.println("******");
}
System.out.println("循环结束");
}
// test
int[] arr = {100,200,300,400,500};
for(int i = 0;i < arr.length;i++) {
System.out.println(arr[i]);
}
}
//结果
******
******
******
******
循环结束
100
200
300
400
500
增强for循环
package com.lemon.operator;
public class for_demo {
public static void main(String[] args) {
int[] arr = {100,200,300,400,500};
// 增强for循环,只能循环数组和集合
// 缺点,没有索引,只能从头遍历到尾
// 优点:代码少
// arr数组的元素是int类型,所以是int
for(int element:arr) {
System.out.println(element);
}
System.out.println("==============");
// arr2数组的元素是string类型,所以for循环里是String
String[] arr2 = {"ab","11","00"};
for(String str :arr2) {
System.out.println(str);
}
}
}
//结果
100
200
300
400
500
==============
ab
11
00
求和思想
package com.lemon.operator;
public class ForDemo3 {
public static void main(String[] args) {
//求和思想
//求1-10的和
int sum = 0;
for(int i =1 ;i<= 10;i++) {
sum += i;
System.out.println(sum);
}
System.out.println(sum);
}
}
//结果
55
while循环
package com.lemon.operator;
public class whileDemo {
public static void main(String[] args) {
// while 输出 1-5
int i = 1;
while(i < 5) {
System.out.println(i);
i++;
}
}
}
//结果
1
2
3
4
网友评论