forked from DuGuQiuBai/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForTest3.java
More file actions
43 lines (33 loc) · 645 Bytes
/
ForTest3.java
File metadata and controls
43 lines (33 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class ForTest3 {
public static void main(String[] args) {
//求出1-100之间偶数和
/*
//定义求和变量
int sum = 0;
//通过for循环获取每一个数据
for(int x=1; x<=100; x++) {
//把数据累加
sum += x;
}
//输出结果
System.out.println("1-100之和:"+sum);
System.out.println("---------------");
*/
//偶数:能被2整除的数据
//如何判断数据是否能够被整出呢? x%2 == 0
/*
int sum = 0;
for(int x=1; x<=100; x++) {
if(x%2 == 0) {
sum += x;
}
}
System.out.println("1-100的偶数和:"+sum);
*/
int sum = 0;
for(int x=0; x<=100; x+=2) {
sum += x;
}
System.out.println("1-100的偶数和:"+sum);
}
}