標籤:天天向上 *** 變化 do while 檢查 條件判斷 ati rgs 條件
第五章 迴圈結構
1.while迴圈
while(條件){
//代碼塊
}
解釋:如果條件的結果為true,那麼執行代碼塊,
當代碼塊執行完後,又會執行條件,
依次迴圈,直到結果為false,整個迴圈結束。
2.程式調試
第一步:設定斷點(左鍵雙擊)
第二步:debug方式好執行代碼
第三步:逐條執行代碼調試(F6)
第四步:檢查代碼(看變數值的變化)
注意:在右上方可以切換java視圖和debug視圖.
3.do while迴圈
do{
//代碼塊
}while(條件)
解釋:先執行代碼塊,然後進行條件判斷
如果條件為true,那麼繼續執行代碼塊
一次迴圈,直到條件為false,整個迴圈
結束.
注意:變數的範圍
(從變數聲明開始到它所在代碼塊結束)
4.while 和 do...while區別
a.執行順序不一樣
b.do...while中的迴圈體至少執行一次,
而while中的迴圈體在結果為false時,
不會被執行。
public class *** { public static void main(String[] args) { int i = 1; while(i<=100){ System.out.println("第"+i+"次好好學習,天天向上"); i++; } }}public class ***{ public static void main(String[] args) { Scanner console = new Scanner(System.in); //條件 人數>1000000 //迴圈 count = count*(1+0.25); int num = 1 ; int count = 250000; while(count<=1000000){ count = (int)(count*(1+0.25)); num++; } System.out.println(2012+num); } }public class ***{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("請輸入攝氏度:"); double xy = input.nextDouble(); int count = 0; do{ double h = xy*9/5.0+32; System.out.println(xy+"對應的華氏:"+h); xy = xy+20; count++; }while(count<10&&xy<=250); }}
java:第五章