do----while 循环
do{
循环内容
}while(循环条件);
for 循环
for(表达式1;表达式2;表达式3){
//循环体
}
示例:100以内的奇数和
public class Test09 {
public static void main(String[] args) {
int sum = 0;
for (int num = 1; num <= 100; num++) {
sum += num;
num = num + 1;
}
System.out.println(sum);
}
}
100以内偶数和:
public class Test09 {
public static void main(String[] args) {
int sum = 0;
int num = 2;
while (num <= 100) {
sum += num;
num = num + 2;
}
System.out.println(sum);
}
}
时间: 2024-10-08 21:22:31