forked from bjmashibing/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileDemo.java
More file actions
61 lines (52 loc) · 892 Bytes
/
WhileDemo.java
File metadata and controls
61 lines (52 loc) · 892 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
循环结构:
1、while 先进行判断,再进行逻辑执行
需要四部分组成
初始化:变量的初始化
条件判断:必须要求返回true或者false的值
循环体:具体的要执行的逻辑代码
迭代变量:促使此循环结束
2、do while 先执行代码逻辑,再执行判断
*/
public class WhileDemo{
public static void main(String [] args){
//while循环样例
/*
int i = 1;
while(i<=100){
System.out.println("第"+i+"遍输出");
i++;
}
*/
//求100内的偶数和
/*
int i = 1;
//求和最终的存储变量
int sum = 0;
while(i<=100){
if(i % 2 == 0){
sum+=i;
}
i++;
}
System.out.println("100以内的偶数和是:"+sum);
*/
// do while
/*
int i = 1;
do{
System.out.println("第"+i+"遍输出");
i++;
}while(i<=100);
*/
int i = 1;
int sum = 0;
do{
if(i % 2 == 0){
sum+=i;
}
i++;
}while(i<=100);
System.out.println("100以内的偶数和是:"+sum);
}
}