一、循環的類型:
1、for循環
1
2
3
4
5
6
7
8
9
10
11
12
|
class for { public static void main(string[] args) { system.out.println( "hello world!" ); system.out.println( "hello world!" ); system.out.println( "hello world!" ); system.out.println( "hello world!" ); system.out.println( "我是分隔符~~~~~~~~~~~~~~~~~~~~~~~~" ); for ( int i = 0 ; i < 4 ; i++){ system.out.println( "hello world!" ); } } } |
運行結果:
2、while() {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class testwhile { public static void main(string[] args) { //100以內的偶數的輸出 int i = 1 ; int sum = 0 ; while (i <= 100 ){ if (i % 2 == 0 ){ system.out.println(i); sum += i; } i++; } system.out.println(sum); //system.out.println(i); } } |
運行結果:
3、do{}while()
1
2
3
4
5
6
7
8
9
10
11
|
class dowhile{ public static void main(string[] args) { int i = 1 ; do { if (i % 2 == 0 ){ system.out.print(i + "\t" ); } i++; } while (i <= 100 ); } } |
運行結果:
二、格式:
所有的循環結構都必須包含以下4部分:
1、初始化條件;
2、循環條件;
3、迭代條件;
4、循環體;
1、for循環格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/* 所有的循環結構都必須包含以下4部分: 1、初始化條件; 2、循環條件; 3、迭代條件; 4、循環體; 在這段代碼中與格式的對應關系為: 1、初始化條件 = int i = 0; 2、循環條件 = i < 4; 3、迭代條件 = i++; 4、循環體 = system.out.println("hello world!"); */ class for { public static void main(string[] args) { for ( int i = 0 ; i < 4 ; i++){ system.out.println( "hello world!" ); } } } |
2、while循環格式:
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
|
/* 所有的循環結構都必須包含以下4部分: 1、初始化條件; 2、循環條件; 3、迭代條件; 4、循環體; 在這段代碼中與格式的對應關系為: 1、初始化條件 = int i = 1;int sum = 0;; 2、循環條件 = i <= 100; 3、迭代條件 = i++; 4、循環體 = if語句; */ class testwhile { public static void main(string[] args) { //100以內的偶數的輸出 int i = 1 ; int sum = 0 ; while (i <= 100 ){ if (i % 2 == 0 ){ system.out.print(i + "\t" ); sum += i; } i++; } system.out.print(sum); } } |
3、do{4 3}while(2);
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
|
/* 所有的循環結構都必須包含以下4部分: 1、初始化條件; 2、循環條件; 3、迭代條件; 4、循環體; 在這段代碼中與格式的對應關系為: 1、初始化條件 = int i = 1; 2、循環條件 = i <= 100; 3、迭代條件 = i++; 4、循環體 = if語句; */ class testdowhile{ public static void main(string[] args) { int i = 1 ; do { if (i % 2 == 0 ){ system.out.println(i); } i++; } while (i <= 100 ); int j = 10 ; do { system.out.println(j); j++; } while (j< 10 ); while (j < 10 ){ system.out.println(j); j++; } } } |
注意:
1、不同的循環結構之間可以相互轉換;
2、while和do-while的區別:do-while程序至少會執行一次;
三、嵌套循環:
說明:循環結構中還可以聲明循環;讓內層循環結構整體充當外層循環的循環體;若外層循環執行m次,內層循環執行n次,整個程序執行m*n次。
可以理解為外層循環控制行數,內層循環控制列數;
例:
1
2
3
4
5
6
7
8
9
10
|
class testfor2 { public static void main(string[] args) { for ( int j = 0 ;j < 4 ;j++){ //外層循環控制行數 for ( int i = 0 ;i < 5 ;i++){ //內層循環控制列數 system.out.print( "*" ); } system.out.println(); } } } |
運行結果:
練習題
1、九九乘法表
1
2
3
4
5
6
7
8
9
10
|
class testjiujiu { public static void main(string[] args) { for ( int i = 1 ;i <= 9 ;i++){ for ( int j = 1 ;j <= i;j++){ system.out.print(i + "*" + j + "=" + i*j + "\t" ); } system.out.println(); } } } |
運行結果:
2、輸出100內的質數(兩種方法實現)
第一種:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class testprimenumber { public static void main(string[] args) { boolean flag = false ; long start = system.currenttimemillis(); for ( int i = 2 ;i <= 100000 ;i++){ //實現100以內的自然數的遍歷 //如何判斷i是否為一個質數 for ( int j = 2 ;j <= math.sqrt(i);j++){ if (i % j == 0 ){ flag = true ; break ; } } if (!flag){ system.out.println(i); } flag = false ; } long end = system.currenttimemillis(); system.out.println( "所花費的時間為:" + (end - start)); } } |
運行結果:由于數據過多,這里使用運營時間來表示
第二種:這種方式主要是為了顯示運行的效率,這里也是使用運行時間來表示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class testprimenumber1 { public static void main(string[] args) { //boolean flag = false; long start = system.currenttimemillis(); l: for ( int i = 2 ;i <= 100000 ;i++){ //實現100以內的自然數的遍歷 //如何判斷i是否為一個質數 for ( int j = 2 ;j <= math.sqrt(i);j++){ if (i % j == 0 ){ //flag = true; //break; continue l; } } //if(!flag){ //system.out.println(i); //} //flag = false; } long end = system.currenttimemillis(); system.out.println( "所花費的時間為:" + (end - start)); } } |
運行結果:
四、無限循環
當需要使用無限循環時,將循環的循環條件修改為true即可(代碼格式請參考第二部分),但是需要注意的是,在無限循環結果內部一定要提供循環的終止條件(使用break關鍵字)否則程序將無限制的執行下去,形成死循環;
五、break和continue:
1、break:
1、使用在swich-case結構或者循環結構中;
2、在循環結構中,一旦執行到break,就跳出當前循環。
2、continue:
1、使用在循環結構中;
2、在循環結構中,一旦執行到continue就跳出當次循環;
3、在嵌套循環中,可以使用帶標簽的break和continue。
例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class testprimenumber1 { public static void main(string[] args) { //boolean flag = false; long start = system.currenttimemillis(); l: for ( int i = 2 ;i <= 100000 ;i++){ //實現100以內的自然數的遍歷 //如何判斷i是否為一個質數 for ( int j = 2 ;j <= math.sqrt(i);j++){ if (i % j == 0 ){ //flag = true; //break; continue l; } } //if(!flag){ //system.out.println(i); //} //flag = false; } long end = system.currenttimemillis(); system.out.println( "所花費的時間為:" + (end - start)); } } |
注意:請注意第5行代碼(l:for(int i = 2;i <= 100000;i++))以及第11行代碼(continue l;),在第五行代碼前邊寫了一個l:的標簽,然后在第11行代碼處進行調用,如果程序執行到這里會自動跳出此循環,然后從第五行開始執行;
六、數組:
1、定義:相同數據類型的數據的組合。
不使用數組的定義方式:
1
2
3
|
int i1 = 1 ; int i2 = 2 ; int i3 = 3 ; |
使用數組的定義方式:
1、靜態初始化:在聲明并初始化數組與給數組相應的元素賦值操作同時進行;
int[] scores = new int[]{72,90,59};
2、動態初始化:在聲明并初始化數組與給數組相應的元素賦值操作分開進行;
int[] scores1 = new int[3];
socres1[0] = 72;
socres1[1] = 90;
socres1[2] = 59;
2、數組的初始化問題(以下的初始化為錯誤的初始化方式):
string[] names = new string[5]{"aa","bb","cc"}
int i = new int[10];
int i = new int[];
注意:不管是動態初始化還是靜態初始化,一定要在創建的時候就指明數組的長度;
3、數組的引用:
1、通過數組下角標的方式來進行引用;下角標從0開始到n-1結束,其中n為數組的長度。
2、數組的長度通過length屬性來調用;
代碼
3、如何遍歷數組:使用循環來進行遍歷
for(int i = 0,i < scores1.length;i++){
system.out.println(scores1[i]);
}
代碼展示:
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
|
public class testarray { public static void main(string[] args){ int i1; i1 = 12 ; boolean b = true ; //1.如何定義一個數組 //1.1數組的聲明 string[] names; int [] scores; //1.2初始化 //第一種:靜態初始化:初始化數組與給數組元素賦值同時進行。 names = new string[]{ "張三" , "李四" , "王五" }; //第二種:動態初始化:初始化數組與給數組元素賦值是分開進行的; scores = new int [ 4 ]; //2.如何調用相應的數組元素:通過數組元素的下角標的方式來調用。 //下角標從0開始,到n-1結束。其中n表示的是數組的長度。 scores[ 0 ] = 87 ; scores[ 1 ] = 89 ; scores[ 3 ] = 98 ; //3。數組的長度:通過數組的length屬性。 system.out.println(names.length); system.out.println(scores.length); //4.如何遍歷數組元素 // system.out.println(names[0]); // system.out.println(names[1]); // system.out.println(names[2]); for ( int i = 0 ;i < names.length;i++){ system.out.println(names[i]); } } } |
以上這篇java中的循環筆記整理(必看篇)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。