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