3、Java中的迴圈筆記,java迴圈筆記
一、迴圈的類型:
1、for迴圈
1 class For{ 2 public static void main(String[] args) { 3 System.out.println("Hello World!"); 4 System.out.println("Hello World!"); 5 System.out.println("Hello World!"); 6 System.out.println("Hello World!"); 7 System.out.println("我是分隔字元~~~~~~~~~~~~~~~~~~~~~~~~"); 8 for(int i = 0; i < 4; i++){ 9 System.out.println("Hello World!");10 }11 }12 }
運行結果:
2、while() {}
1 class TestWhile { 2 public static void main(String[] args) { 3 //100以內的偶數的輸出 4 int i = 1; 5 int sum = 0; 6 while(i <= 100){ 7 if(i % 2 == 0){ 8 System.out.println(i); 9 sum += i;10 }11 i++;12 }13 System.out.println(sum);14 //System.out.println(i);15 }16 }
運行結果:
3、do{}while()
1 class DoWhile{ 2 public static void main(String[] args) { 3 int i = 1; 4 do{ 5 if(i % 2 == 0){ 6 System.out.print(i + "\t"); 7 } 8 i++; 9 }while(i <= 100);10 }11 }
運行結果:
二、格式:
所有的迴圈結構都必須包含以下4部分:
1、初始化條件;
2、迴圈條件;
3、迭代條件;
4、迴圈體;
1、for迴圈格式:
1 /* 2 所有的迴圈結構都必須包含以下4部分: 3 1、初始化條件; 4 2、迴圈條件; 5 3、迭代條件; 6 4、迴圈體; 7 在這段代碼中與格式的對應關係為: 8 1、初始化條件 = int i = 0; 9 2、迴圈條件 = i < 4;10 3、迭代條件 = i++;11 4、迴圈體 = System.out.println("Hello World!"); 12 */13 class For{14 public static void main(String[] args) {15 for(int i = 0; i < 4; i++){16 System.out.println("Hello World!");17 }18 }19 }
2、while迴圈格式:
1 /* 2 所有的迴圈結構都必須包含以下4部分: 3 1、初始化條件; 4 2、迴圈條件; 5 3、迭代條件; 6 4、迴圈體; 7 在這段代碼中與格式的對應關係為: 8 1、初始化條件 = int i = 1;int sum = 0;; 9 2、迴圈條件 = i <= 100;10 3、迭代條件 = i++;11 4、迴圈體 = if語句; 12 */13 class TestWhile {14 public static void main(String[] args) {15 //100以內的偶數的輸出16 int i = 1;17 int sum = 0;18 while(i <= 100){19 if(i % 2 == 0){20 System.out.print(i +"\t");21 sum += i;22 }23 i++;24 }25 System.out.print(sum);27 }28 }
3、do{4 3}while(2);
/*所有的迴圈結構都必須包含以下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 class TestFor2 { 2 public static void main(String[] args) { 3 for(int j = 0;j < 4;j++){//外層迴圈控制行數 4 for(int i = 0;i < 5;i++){//內層迴圈控制列數 5 System.out.print("*"); 6 } 7 System.out.println(); 8 } 9 }10 }
運行結果:
練習題
1、九九乘法表
1 class TestJiuJiu { 2 public static void main(String[] args) { 3 for(int i = 1;i <= 9;i++){ 4 for(int j = 1;j <= i;j++){ 5 System.out.print(i + "*" + j + "=" + i*j + "\t"); 6 } 7 System.out.println(); 8 } 9 }10 }
運行結果:
2、輸出100內的質數(兩種方法實現)
第一種:
1 class TestPrimeNumber { 2 public static void main(String[] args) { 3 boolean flag = false; 4 long start = System.currentTimeMillis(); 5 for(int i = 2;i <= 100000;i++){//實現100以內的自然數的遍曆 6 //如何判斷i是否為一個質數 7 for(int j = 2;j <= Math.sqrt(i);j++){ 8 if(i % j == 0){ 9 flag = true;10 break;11 }12 }13 if(!flag){14 System.out.println(i);15 }16 flag = false;17 }18 long end = System.currentTimeMillis();19 System.out.println("所花費的時間為:" + (end - start));20 }21 }
運行結果:由於資料過多,這裡使用運營時間來表示
第二種:這種方式主要是為了顯示啟動並執行效率,這裡也是使用已耗用時間來表示。
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 class TestPrimeNumber1 { 2 public static void main(String[] args) { 3 //boolean flag = false; 4 long start = System.currentTimeMillis(); 5 l:for(int i = 2;i <= 100000;i++){//實現100以內的自然數的遍曆 6 //如何判斷i是否為一個質數 7 for(int j = 2;j <= Math.sqrt(i);j++){ 8 if(i % j == 0){ 9 //flag = true;10 //break;11 continue l;12 }13 }14 //if(!flag){15 //System.out.println(i);16 //}17 //flag = false;18 }19 long end = System.currentTimeMillis();20 System.out.println("所花費的時間為:" + (end - start));21 }22 }
注意:請注意第5行代碼(l:for(int i = 2;i <= 100000;i++))以及第11行代碼(continue l;),在第五行代碼前邊寫了一個l:的標籤,然後在第11行代碼處進行調用,如果程式執行到這裡會自動跳出此迴圈,然後從第五行開始執行;
六、數組:
1、定義:相同資料類型的資料的組合。
不使用數組的定義方式:
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 public class TestArray { 2 public static void main(String[] args){ 3 int i1; 4 i1 = 12; 5 boolean b = true; 6 //1.如何定義一個數組 7 //1.1數組的聲明 8 String[] names; 9 int[] scores;10 //1.2初始化11 //第一種:靜態初始化:初始化數組與給數組元素賦值同時進行。12 names = new String[]{"張三","李四","王五"};13 //第二種:動態初始化:初始化數組與給數組元素賦值是分開進行的;14 scores = new int[4];15 //2.如何調用相應的數組元素:通過數組元素的下角標的方式來調用。16 //下角標從0開始,到n-1結束。其中N表示的是數組的長度。17 scores[0] = 87;18 scores[1] = 89;19 scores[3] = 98;20 //3。數組的長度:通過數組的length屬性。21 System.out.println(names.length);22 System.out.println(scores.length);23 //4.如何遍曆數組元素24 // System.out.println(names[0]);25 // System.out.println(names[1]);26 // System.out.println(names[2]);27 for(int i = 0;i < names.length;i++){28 System.out.println(names[i]);29 }30 }31 }