Files
javaSE-0113/day03/src/com/inmind/for03/Test08.java

28 lines
999 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.for03;
//循环流程-for循环_偶数累和
public class Test08 {
//使用循环计算1-100之间的偶数和
/*
实现步骤:
1.定义一个变量,用来记录总和
2.使用for实现1~100的值的变化
3.在for的循环体中判断对应的值是否是偶数如果是则累加到总和变量中
4.循环结束,打印最终结果
*/
public static void main(String[] args) {
//1.定义一个变量,用来记录总和
int sum = 0;
//2.使用for实现1~100的值的变化
for (int i = 1; i <= 100; i++) {
//3.在for的循环体中判断对应的值是否是偶数如果是则累加到总和变量中
if (i % 2 == 0) {//判断是否是偶数
// sum = sum + i;
sum += i;
}
}
//4.循环结束,打印最终结果
System.out.println("1~100的偶数和为"+sum);
}
}