大齡屌絲自學筆記--Java零基礎到菜鳥--011

來源:互聯網
上載者:User

標籤:

迴圈語句:for 、 while...do 、 do...while

1、for:(初始化語句;判斷條件陳述式;控制條件陳述式){ 迴圈體語句; }

1 class Fin{2     public static void main(String[] args){3         for(int i=0;i<10;i++){4             System.out.println("Hello World");5         }6     }7 }8 9 //i初始值為0,小於10,輸出第1個 “Hello World”;然後依次執行 “i++” ,直到i=10的時候,不滿足 “i<10” 這一控制條件,結束迴圈,一共輸出10個 “Hello World”

 

水仙花數:各位元的立方和等於其本身的三位元,如:153=1*1*1+5*5*5+3*3*3=153。

 1 class Fin{ 2     public static void main(String[] args){ 3         for(int i=100;i<1000;i++){ 4             int a=x%10;       //個位 5             int b=(x/10)%10;  //十位 6             int c=x/100;      //百位 7  8             if((a*a*a+b*b*b+c*c*c)==i){ 9                 System.out.println(i);  //輸出水仙花數10             }11         }12     }13 }

 

2、while...do:while(判斷條件陳述式){迴圈體語句; 控制條件陳述式;}

 1 class Fin{ 2     public static void main(String[] args){ 3         int n=0; 4         while(n<10){ 5             System.out.println("Hello World"); 6             n++ 7         } 8     } 9 }10 11 //輸出10個 “Hello World”

 

3、do...while:do{迴圈體語句; 控制條件陳述式;}while(判斷條件陳述式)

 1 class Fin{ 2     public static void main(String[] args){ 3         int n=0; 4         do{ 5             System.out.println("Hello World"); 6             n++ 7         }while(n<10) 8     } 9 }10 11 //輸出10個 “Hello World”

 

大齡屌絲自學筆記--Java零基礎到菜鳥--011

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.