private static void testWhile() { int i = 1; int sum = 0; while (i <= 10) { sum += i; i++; } do { sum += i; i++; } while (i <= 10); System.out.println("sum:" + sum); }
for
1 2 3 4 5 6 7 8 9
private static void testFor() { for (int i = 1; i < 20; i++) { if (i % 2 == 0) { System.out.println(i + "是偶数"); continue; } System.out.println(i + "是奇数"); } }
switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14
private static void testSwitch() { int sex = 0; switch (sex) { case 0: System.out.println("男性"); break; case 1: System.out.println("男性"); break; default: System.out.println("未知"); break; } }