java:第五章

來源:互聯網
上載者:User

標籤:天天向上   ***   變化   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:第五章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.