-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoop.java
More file actions
101 lines (93 loc) · 2.22 KB
/
Copy pathLoop.java
File metadata and controls
101 lines (93 loc) · 2.22 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package base;
/**
*
*/
public class Loop {
public static void main(String[] args) {
whileJob();
dowhileJob(10);
forJob();
night2();
peach();
}
/**
* while( 布尔表达式 ) {
* //循环内容
* }
*/
public static void whileJob() {
int x = 0;
while (x < 10) {
System.out.println("while: " + x);
x++;
}
}
/**
* do {
* //代码语句
* }while(布尔表达式);
*/
public static void dowhileJob(int x) {
do {
System.out.println("do while: " + x);
x++;
} while (x < 20);
}
/**
* for(初始化; 布尔表达式; 更新) {
* //代码语句
* }
*/
public static void forJob() {
for (int x = 1; x < 10; x=x+1) {
System.out.println("for: " + x);
}
for (int x = 11; x < 20;) {
System.out.println("for: " + x);
x++;
}
int []numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
// continue;
}
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String []names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
System.out.print("\n");
}
public static void night2() {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
if ((i==3||i==4) && (j==3)){
System.out.print(" "+j+"*"+i+"="+i*j+" ");
}else {
System.out.print(j+"*"+i+"="+i*j+" ");
}
}
System.out.println();
}
}
public static void peach(){
// 方法一
int sum1=1;
for(int i=9;i>=1;i--){
sum1=(sum1+1)*2;
}
System.out.println("sum1="+sum1);
// 方法二
int sum2=1;
for (int i=1;i<=9;i++){
sum2=(sum2+1)*2;
}
System.out.println("sum2="+sum2);
}
}