一、While循環語句
1、格式
while(條件表達式){
執行語句;
}
2、要點
3、舉例
題目1:輸出0-100之間的所有數
1 class Demo3 2 { 3 public static void main(String[] args){ 4 //輸出0-100之間的所有數 5 int i=1; 6 while(i<100){ 7 System.out.println(i); 8 i++; 9 } 10 } 11 }
題目2:猜數字游戲
1 public static void main(String[] args) { 2 //解析:Math.random()返回一個double類型的0.0~1.0之間的數值; Math.random()*100返回結果:0.0~100.0(不包括0.0和100.0); (int)(Math.random()*100)+1,返回結果:1~100(包括1和100) 3 int num = (int)(Math.random()*100)+1; // 在1~100之間,生成一個隨機數。 4 Scanner sc = new Scanner(System.in); //創建一個掃描器對象 5 int guessNum = -1; // guessNum 的初始化值不能在1~100之間(包括1和100)。 6
7 while (guessNum != num) { 8 System.out.println("請輸入1-100之間整數"); 9
10 int guessNum = sc.nextInt(); //掃描你輸入的整形對象 11 if (guessNum == num) { 12 System.out.println("中啦"); 13 } else if (guessNum < num) { 14 System.out.println("小啦"); 15 } else { 16 System.out.println("大了"); 17 } 18 } 19 }
二、do ... while循環語句
1、格式
do{
執行相應的代碼;
}while(判斷條件);
2、要點
3、while循環語句和do-while循環語句的區別:
1)while循環語句,是先判斷,再執行相應代碼
2)do-while循環語句,先執行相應的代碼,再判斷
4、例子
1 public static void main(String[] args) { 2 int x = 0, y = 0; 3 do { 4 System.out.println(x); 5 x++; 6 } while (x < 0);//注意一個細節:do...while 后面的分號 7 // do...while:不管是否滿足循環條件,do都會先執行一次。 8 while (y < 0) { 9 System.out.println(y); //條件不滿足,結果只有x的值輸出 10 y++; 11 } 12 }
題目2:猜數字游戲
1 public static void main(String[] args) { 2 // 記錄用戶輸入的數字 3 int guess = -1; 4 // 記錄用戶輸入次數 5 int count = 0; 6 // 生成1-100之間隨機數 7 int num = (int) (int)(Math.random()*100)+1;// 在1~100之間,生成一個隨機數。 8 Scanner sc = new Scanner(System.in);//創建一個掃描器對象 9 10 // 循環猜數字 11 do { 12 System.out.println("請輸入1-100之間的數字"); 13 guess = sc.nextInt();//掃描你輸入的整形對象 14 if (guess > num) { 15 System.out.println("哥們,太大了"); 16 } else if (guess < num) { 17 System.out.println("哥們,太小了"); 18 } else { 19 System.out.println("恭喜您,中啦"); 20 } 21 count++; 22 } while (guess != num); //注意一個細節:do...while 后面的分號 23 System.out.println("你猜測的數字是:" + num + "猜測了" + count + "次"); 24 }
三、for 循環語句(和增強for循環)
1.格式
for(初始化表達式;循環條件表達式;循環后的操作表達式){
執行語句;
}
2、for 和while的區別
1 public static void main(String[] args) { 2 for (int x = 0; x < 5; x++) { 3 System.out.println("hello java"); 4 } 5 System.out.println(x); 6 //x cannot be resolved to a variable 7 8 int y = 0; 9 while (y < 5) { 10 System.out.println("hello world"); 11 y++; 12 } 13 System.out.println(y); 14 }
解析:
x 為什么會找不到,注意了變量的作用域,也就是變量的作用范圍。x 只在 for 循環的大括號內有效,出了這個區域,就無效了.在內存中就消失了。x消失后,仍要訪問它,肯定會報錯的。
y 就不一樣了,y 是定義在while 外的。while循環完畢仍有效 while的初始化 動作在外邊,循環結束后y 仍然存在。
當定義的y 只作為循環增量存在的話的,循環完畢后y就沒有用了,但是y還是占着一塊內存。所以,如果定義的變量只作為循環增量存在的話,就用for 循環可以節約內存。
其實for 和while 是可以互換的。
總結
A、for里面的兩個表達式運行的順序,初始化表達式只讀一次,判斷循環條件,為真就執行循環體,然后再執行循環后的操作表達式,接着繼續判斷循環條件,重復找個過程,直到條件不滿足為止。
B、while與for可以互換,區別在於for為了循環而定義的變量在for循環結束時就在內存中釋放。而while循環使用的變量在循環結束后還可以繼續使用。
C、最簡單無限循環格式:while(true) , for(;;),無限循環存在的原因是並不知道循環多少次,而是根據某些條件,來控制循環。推薦使用while(true)
3、舉例
題目1:打印99乘法表
1 class Demo1{ 2 3 public static void main(String[] args) { 4 for (int x = 1; x <= 9; x++) { 5 for (int y = 1; y <= x; y++) { 6 System.out.print(y + "*" + x + "=" + x * y + '\t'); // \t:制表符,相當於Table鍵,一個\t代表四個空格 7 } 8 System.out.println(" "); 9 } 10 } 11 }
效果圖:
轉義字符:
\r 表示接受鍵盤輸入,相當於按下回車。
\n 表示換行。
\t 制表符,相當於Table鍵(四個空格)
\b 退格鍵,相當於Back Space
\’ 單引號
\’’ 雙引號
\\ 表示一個斜跨
題目2:打印直角三角形
1 public static void main(String[] args) { 2 for (int x = 0; x < 5; x++) { 3 for (int y = 0; y <= x; y++) { 4 System.out.print("*"); 5 } 6 System.out.println(""); 7 } 8 9 }
效果圖
題目3:打印倒直角三角形
1 public static void main(String[] args) { 2 for (int x = 5; x > 0; x--) { 3 for(int y=x;y>0;y--){ 4 System.out.print("*"); 5 } 6 System.out.println(""); 7 } 8 }
效果圖
4、增強for循環(也叫foreach循環)
jdk1.5出現的新功能
1)增強for循環的作用
簡化了迭代器的書寫格式(注意:增強for循環底層還是使用了迭代器遍歷)
2)增強for循環的適用范圍
如果實現了Iterable接口或者數組對象都可以使用增強for循環.
3)增強for循環的格式
for(數據類型 變量名:遍歷的目標對象名){ }
4)增強for循環需要注意的事項
1、增強for循環底層也是使用迭代器獲取的,只不過獲取迭代器是由jvm完成,不需要我們獲取迭代器,索引在使用增強for循環遍歷元素的過程中不准使用集合對對象對集合元素經行修改
2、迭代器遍歷元素和增強for循環的區別:使用迭代器遍歷元素時可以刪除集合元素,而增強for循環遍歷集合元素時,不能調用迭代器里面的remove方法刪除元素.
3、普通for循環與增強for循環的區別:普通for循環可以沒有變量目標,而增強for循環一定要有變量目標
1 public class Demo2 { 2 public static void main(String[] args) { 3 int[] arr={12,2,5,0}; 4 5 //普通for循環 6 for (int i = 0; i < arr.length; i++) { 7 System.out.println(arr[i]); 8 } 9 10 //增強for循環 11 for (int i : arr) { 12 System.out.println(i); 13 } 14 } 15 }
四、 break、continue關鍵字
1、break關鍵字:break 語句用於終止最近的封閉循環或它所在的 switch 語句。控制傳遞給終止語句后面的語句(如果有的話)。
適用:for循環 、 switch判斷語句。
break的用法:
- 單獨使用。
- 與標簽一起使用。(標簽:即一個名字,滿足標識符的條件即可)。
使用細節: 不要再break語句之后,編寫其他語句,永遠都執行不到,編譯報錯。
2、continue關鍵字:語句將控制權傳遞給它所在的封閉迭代語句的下一次迭代。(跳出本循環,執行下一次循環)。
適用於:while 、 do while 、 for循環語句
使用細節:
1. 如果continue出現在循環的末尾(最后一條語句),那么可以省略。
2. 如果continue出現在循環的第一條語句,那么后面的語句都無法執行,所以編譯報錯。
3. 可以結合標記使用。
3、小結:
原創作者:DSHORE 作者主頁:http://www.cnblogs.com/dshore123/ 原文出自:http://www.cnblogs.com/dshore123/p/8656779.html 歡迎轉載,轉載務必說明出處。(如果本文對您有幫助,可以點擊一下右下角的 推薦,或評論,謝謝!) |