day03-循环流程-for循环_偶数累和

This commit is contained in:
2026-07-16 15:57:43 +08:00
parent 73a7ece68b
commit 95c80e4646

View File

@@ -0,0 +1,23 @@
package com.inmind.for03;
/*
使用循环计算1-100之间的偶数和
分析:
1.1~100 for循环
2.偶数和,%2 == 0 就是偶数
3.如果是偶数,累加
*/
public class Demo06 {
public static void main(String[] args) {
int sum = 0;//用来进行累加得结果
for (int i = 1; i <= 100; i++) {//1~100 for循环
//判断是否是偶数,是则累加,必定要赋值给一个变量
if (i % 2 == 0) {
//是偶数,就累加
sum = sum + i;
}
}
System.out.println("1~100的偶数累加的结果为"+sum);
}
}