# 第 1 題 - I (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { int m = 3; int n = 2; for (int i = 0; i < m * n; i++) // 執行 i = 0 ~ (m * n - 1) 次 { System.out.print("A"); // 共執行 m * n 次 } } } # 第 1 題 - II (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { int m = 3; int n = 2; for (int j = 1; j <= m; j++) // 執行 m 次 { for (int k = 1; k < n; k++) // 執行 n - 1 次 { System.out.print("B"); // 共執行 m * (n - 1) 次 } } } } # 第 10 題 // 0 2 0 2 0 2 0 要由 k = 0, 2, 3, 5, 6, 8, 9 來產生 public class Main { public static void main(String args[]) { int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) // 產生 k = 0, 2, 3, 5, 6, 8, 9 k = k + 2; else k++; } } } # 第 11 題 // 和 = ((首項 + 末項)* 項數) / 2 = ((1 + 101) * 51) / 2 = 2601 // 項數 = ((末項 - 首項) / 公差) + 1 = ((101 - 1) / 2) + 1 = 51 public class Main { public static void main(String args[]) { int r = 0; int sum = 0; while (r <= 101) { if (r % 2 == 1) { sum += r; } r++; } System.out.println(sum); } } # 第 12 題 public class Main { public static void main(String args[]) { int x = 1; while (x < 0) { if (x % 2 == 0) { System.out.print(x + " "); } x = x + 2; } } } # 第 13 題 (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { for (int outer = 0; outer < 3; outer++) { for (int inner = outer; inner < 3; inner++) { System.out.print(outer + "" + inner + "_"); } } } } # 第 14 題 - I (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { int x = 1; while (x < 6) { System.out.print(x + " "); x = x + 2; } } } # 第 14 題 - II (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { int x = 1; while (x < 7) { System.out.print(x + " "); x = x + 2; } } } # 第 15 題 (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { int val = 48; int div = 6; while ((val % 2 == 0) && div > 0) { if (val % div == 0) { System.out.print(val + " "); } val /= 2; div--; } } } # 第 16 題 public class Main { public static void main(String args[]) { String new_text = mystery("computer"); System.out.println(new_text); } public static String mystery(String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; } } # 第 17 題 public class Main { public static void main(String args[]) { int start = 4; int end = 5; boolean keepGoing = true; if (start < end && keepGoing) { if (end > 0) { start += 2; end++; } else { end += 3; } } if (start < end) { if (end == 0) { end += 2; start++; } else { end += 4; } } System.out.println(end); } } # 第 18 題 (*** 10/15 已說明 ***) public class Main { public static void main(String args[]) { for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); } } } # 第 22 題 import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.print("Input a number: "); int num = scanner.nextInt(); if (num > 0) { if (num % 2 == 0) { System.out.println("A"); } else { System.out.println("B"); } } } } # 第 25 題 public class Main { public static void main(String args[]) { int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y); } } # 第 27 題 public class Main { public static void main(String args[]) { String new_str = abMethod("sing the song", "ng"); System.out.println(new_str); } public static String abMethod(String a, String b) { int x = a.indexOf(b); while (x >= 0) { a = a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return a; } } # 第 30 題 public class Main { public static void main(String args[]) { int num1 = 0; int num2 = 3; while ((num2 != 0) && ((num1 / num2) >= 0)) { num1 = num1 + 2; num2 = num2 - 1; } System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); } }