while package ch4; /** * Created by Jiqing on 2016/11/6. */ public class While { public static void main(String[] args) { int count =0; while(count <10) { System.out.println(count); count++; } // 输出0~9 System.out.println("循环结束"); } } doWhile package ch4; /** * Created by Jiqing on 2016/11/6. */ public class doWhile { public static void main(String[] args) { int count = 1; // 至少执行一次 do { System.out.println(count); count++; } while (count < 10);// 0~9 System.out.println("循环结束"); } } For package ch4; /** * Created by Jiqing on 2016/11/6. */ public class For { public static void main(String[] args) { for(int count = 0; count <10 ;count++) { System.out.println(count); } System.out.println("循环结束!"); int count = 0; for (; count < 10 ;) { System.out.println(count); count++; } System.out.println("循环结束!"); } } Continue package ch4; /** * Created by Jiqing on 2016/11/6. */ public class Continue { public static void main(String[] args) { for (int i = 0;i<5;i++) { System.out.println("i的值"+i); if (i == 1) { continue; } System.out.println("continue后的语句"+i); /** i的值0 continue后的语句0 i的值1 i的值2 continue后的语句2 i的值3 continue后的语句3 i的值4 continue后的语句4 */ } outer: for (int i = 0;i<5;i++) { for (int j = 0;j<3;j++) { System.out.println("i的值"+i+",j的值"+j); if (j == 1) { continue outer; } } } /** i的值0,j的值0 i的值0,j的值1 i的值1,j的值0 i的值1,j的值1 i的值2,j的值0 i的值2,j的值1 i的值3,j的值0 i的值3,j的值1 i的值4,j的值0 i的值4,j的值1 */ } } Break package ch4; /** * Created by Jiqing on 2016/11/6. */ public class Break { public static void main(String[] args) { for (int i=0;i<10;i++) { System.out.println("i的值是"+i); if (i == 2) { break; // 跳出该循环 } } /** i的值是0 i的值是1 i的值是2 */ outer: for (int i = 0;i<5;i++) { for (int j = 0;j<3;j++) { System.out.println("i的值"+i+",j的值"+j); if (j == 1) { break outer; } } } /** i的值0,j的值0 i的值0,j的值1 */ } } Return package ch4; /** * Created by Jiqing on 2016/11/6. */ public class Return { public static void main(String[] args) { for (int i = 0;i<5;i++) { for (int j = 0;j<3;j++) { System.out.println("i的值"+i+",j的值"+j); if (j == 1) { break; // 跳出本次循环 } } } System.out.println("==================="); for (int i = 0;i<5;i++) { for (int j = 0;j<3;j++) { System.out.println("i的值"+i+",j的值"+j); if (j == 1) { return; // 直接结束循环,相当于break outer } } } } } // i的值0,j的值0 // i的值0,j的值1 // i的值1,j的值0 // i的值1,j的值1 // i的值2,j的值0 // i的值2,j的值1 // i的值3,j的值0 // i的值3,j的值1 // i的值4,j的值0 // i的值4,j的值1 // =================== // i的值0,j的值0 // i的值0,j的值1 数组 package ch4; /** * Created by Jiqing on 2016/11/9. */ public class Array { public static void main(String[] args) { int[] intArr; intArr = new int[]{5,4,8,20}; Object[] objArr; objArr = new String[]{"Java","李刚"}; // 定义和初始化同时完成 int[] a = {5,6,7,9}; System.out.println(a[1]); // 6 System.out.println(intArr[1]); // 4 System.out.println(objArr[1]); // 李刚 // 循环输出 for(int i = 0;i<a.length;i++) { System.out.println(a[i]); } for (Object obj : objArr) { System.out.println(obj); } for (int i : a) { System.out.println(i); } } }
疯狂java讲义之流程控制与数组
内容版权声明:除非注明,否则皆为本站原创文章。