-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopTransfer2.java
More file actions
43 lines (37 loc) · 1.01 KB
/
LoopTransfer2.java
File metadata and controls
43 lines (37 loc) · 1.01 KB
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
public class LoopTransfer2 {
public static void main(String[] args) {
// for-loop
System.out.println("for迴圈1到100間3的倍數總合為" + for_loop());
// while-loop
System.out.println("while迴圈1到100間3的倍數總合為" + while_loop());
// do-while-loop
System.out.println("do-while迴圈1到100間3的倍數總合為" + do_while_loop());
}
public static int for_loop() {
int count = 0;
for (int i=1; i <=100; i++)
if (i % 3 == 0)
count += i;
return count;
}
public static int while_loop() {
int count = 0;
int i = 1;
while (i <= 100) {
if (i % 3 == 0)
count += i;
i++;
}
return count;
}
public static int do_while_loop() {
int count = 0;
int i = 1;
do {
if (i % 3 == 0)
count += i;
i++;
} while (i <= 100);
return count;
}
}