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

26 lines
849 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;
/*
使用循环计算1-100之间的偶数和
1. 创建变量,保存偶数和
2. 使用for循环获取1——100之间所有的数(for循环)
3. 判断当前数是偶数if偶数是/2没有余数%
4. 如果是偶数,就累加到偶数和变量中
*/
public class Test08 {
public static void main(String[] args) {
//1. 创建变量,保存偶数和
int sum = 0;
//2. 使用for循环获取1——100之间所有的数(for循环)
for (int i = 1; i <= 100; i++) {
//3. 判断当前数是偶数if偶数是/2没有余数%
if (i % 2 == 0) {
//4. 如果是偶数,就累加到偶数和变量中
sum = sum+i;
}
}
System.out.println("1-100之间的偶数和为"+sum);
}
}