国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java基礎學習筆記2-循環

2019-11-15 00:10:03
字體:
來源:轉載
供稿:網友
java基礎學習筆記2-循環

while循環與do while循環:

while循環的格式:

while(條件表達式)

{

  執行語句;

}

do while循環格式:

do

{

執行語句;

}

while(條件表達式);

do while與while的區別:do while無論條件是否成立都會執行一次語句,即執行do里面的語句,

while則會先判斷表達式是否成立,成立才會執行,不成立直接跳出while循環.

 1 class WhileDemo 2 { 3     public static void main(String args[]) 4     { 5         //打印出1-10的數值 6         int a = 1; 7         while(a<=10)  8         { 9             System.out.
 1 class ForForDemo 2 { 3     public static void main(String args[]) 4     { 5         /* 6         打印出長方形 7         */ 8         for(int i = 0; i < 4; i++) 9         {10             for(int j = 0; j < 3; j++)11             {12                 System.out.print("*");13             }14             System.out.println();15         }16         System.out.println("=======");17         /*18         打印出如下圖形;19         *****20         ****21         ***22         **23         *24         */25         for(int x = 0; x < 5; x++)26         {27             for(int y = 0; y < 5-x; y++)28             {29                 System.out.print("*");30             }31             System.out.println();32         }33         34         for(int a = 0; a < 5; a++)35         {36             for(int b = a; b < 5; b++)  //改變初始化值37             {38                 System.out.print("*");39             }40             System.out.println();41         }42         43         for(int x = 5; x > 0; x--)44         {45             for(int y = 0; y < x; y++)  //改變條件判斷表達式46             {47                 System.out.print("*");48             }49             System.out.println();50         }51         System.out.println("=======");52         53         /*54         打印出如下圖形55         *56         **57         ***58         ****59         *****60         (內循環)61         尖朝上可以改變條件表達式,讓條件隨外循環變化62         尖朝下可以改變初始化表達式,讓初始化隨著外循環變化.63         */64         for(int x = 0; x < 5; x++)65         {66             for(int y = 0; y <= x; y++)67             {68                 System.out.print("*");69             }70             System.out.println();71         }72             73         74         75     }76 }
View Code

用for循環嵌套打印出99乘法表:

 1 /* 2 需求:打印出如下圖像 3 1 4 12 5 123 6 1234 7 12345 8 */ 9 class ForForTest10 {11     public static void main(String args[])12     {13         for(int a =1; a <= 5; a++)14         {15             for(int b =1; b <= a; b++)16             {17                 System.out.print(b);18             }19             System.out.println();20         }    21         System.out.println("==================");22         23         24         /*25             需求:打印出99乘法表26         */27         28         for(int x = 1; x <= 9; x++)29         {30             for(int y = 1; y <= x; y++)31             {32                 System.out.print(x + "*" + y + "=" + x*y + "/t");33             }34             System.out.println();35         }36         System.out.println("=========================");37         38         for(int x = 1; x <= 9; x++)39         {40             for(int y = 1; y <= x; y++)41             {    42                 System.out.print(y+"*"+x+"="+y*x + "/t");43             }44             System.out.println();45         }46     }47 }

打印出等腰三角形(要學會思考,找到寫代碼的思路):

 1 /* 2 需求:打印出如下圖形 3     * 4    * * 5   * * * 6  * * * * 7 * * * * * 8 */ 9 10 11 class ForForTest212 {13     public static void main(String args[])14     {15         for(int x = 0; x < 5; x++)16         {17             for(int a = x; a < 5; a++ )18             {19                 System.out.print(" ");20             }21             for(int y = 0; y <= x; y++)22             {23                 System.out.print("* ");24             }25             System.out.println();26         }27     }28     29     30     31     32     33 }

感覺for循環嵌套好多種寫法,思路很重要,以后要養成先寫思路的編程思想,先把思路寫出來,在慢慢的寫代碼.

努力,才有可能更好.為明天加油.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孝昌县| 施甸县| 大港区| 凯里市| 横山县| 兴国县| 东兰县| 定远县| 互助| 太湖县| 依兰县| 滁州市| 凤台县| 衡阳县| 南皮县| 明光市| 苗栗县| 青海省| 西乡县| 宣汉县| 南阳市| 大石桥市| 海盐县| 孟村| 古蔺县| 辽中县| 岳池县| 华池县| 乐陵市| 平果县| 新沂市| 怀集县| 靖边县| 延津县| 县级市| 西青区| 安丘市| 蒙自县| 营口市| 昌宁县| 册亨县|