
题目考题 1if-else 分支javaimport java.util.Scanner; public class ScoreLevel { public static void main(String[] args) { Scanner sc new Scanner(System.in); System.out.print(请输入成绩0-100); int score sc.nextInt(); if (score 90 score 100) { System.out.println(优秀); } else if (_) { //Found System.out.println(良好); } else if (score 60 score 80) { System.out.println(及格); } else if (score 0 score 60) { System.out.println(不及格); } else { System.out.println(成绩无效); } sc.close(); } }要求填入条件使 80-89 分输出 “良好”。考题 2switch 分支javaimport java.util.Scanner; public class Weekday { public static void main(String[] args) { Scanner sc new Scanner(System.in); System.out.print(请输入星期数1-7); int day sc.nextInt(); switch (day) { case 1: System.out.println(星期一); break; case 2: System.out.println(星期二); break; case 3: System.out.println(星期三); break; case 4: System.out.println(星期四); break; case 5: System.out.println(星期五); break; case 6: System.out.println(星期六); break; case 7: _; //Found break; default: System.out.println(输入错误); } sc.close(); } }要求填入语句使输入 7 时输出 “星期日”。考题 3for 循环javapublic class SumEven { public static void main(String[] args) { int sum 0; // 计算1-100之间所有偶数的和 for (int i _; i 100; i 2) { //Found sum i; } System.out.println(1-100偶数和 sum); // 预期输出2550 } }要求填入 for 循环初始值使循环正确计算 1-100 偶数和。考题 4while 循环javapublic class Factorial { public static void main(String[] args) { int n 5; long fact 1; int i 1; while (_) { //Found fact * i; i; } System.out.println(n 的阶乘是 fact); // 预期输出120 } }要求填入 while 循环条件计算 5 的阶乘5! 5×4×3×2×1。考题 5do-while 循环javaimport java.util.Scanner; public class GuessNumber { public static void main(String[] args) { Scanner sc new Scanner(System.in); int target 7; // 目标数字 int guess; do { System.out.print(请猜一个1-10的数字); guess sc.nextInt(); if (guess target) { System.out.println(猜小了); } else if (guess target) { System.out.println(猜大了); } } while (_); //Found System.out.println(猜对了); sc.close(); } }要求填入 do-while 循环条件使猜对数字7后退出循环。考题 6循环嵌套javapublic class PrintStar { public static void main(String[] args) { // 打印4行由*组成的直角三角形 for (int i 1; i 4; i) { for (int j 1; j _; j) { //Found System.out.print(*); } System.out.println(); } } }要求填入内层循环条件使输出结果为* ** *** ****参考答案考题 1 答案javaimport java.util.Scanner; public class ScoreLevel { public static void main(String[] args) { Scanner sc new Scanner(System.in); System.out.print(请输入成绩0-100); int score sc.nextInt(); if (score 90 score 100) { System.out.println(优秀); } else if (score 80 score 90) { //Found System.out.println(良好); } else if (score 60 score 80) { System.out.println(及格); } else if (score 0 score 60) { System.out.println(不及格); } else { System.out.println(成绩无效); } sc.close(); } }解析需要匹配 80-89 分的区间条件为score 80 score 90确保该分数段输出 “良好”。考题 2 答案javaimport java.util.Scanner; public class Weekday { public static void main(String[] args) { Scanner sc new Scanner(System.in); System.out.print(请输入星期数1-7); int day sc.nextInt(); switch (day) { case 1: System.out.println(星期一); break; case 2: System.out.println(星期二); break; case 3: System.out.println(星期三); break; case 4: System.out.println(星期四); break; case 5: System.out.println(星期五); break; case 6: System.out.println(星期六); break; case 7: System.out.println(星期日); //Found break; default: System.out.println(输入错误); } sc.close(); } }解析case 7对应星期日填入输出语句System.out.println(星期日);即可满足需求。考题 3 答案javapublic class SumEven { public static void main(String[] args) { int sum 0; // 计算1-100之间所有偶数的和 for (int i 2; i 100; i 2) { //Found sum i; } System.out.println(1-100偶数和 sum); // 预期输出2550 } }解析1-100 的第一个偶数是 2循环初始值设为2每次加 2 即可遍历所有偶数累加和为 2550。考题 4 答案javapublic class Factorial { public static void main(String[] args) { int n 5; long fact 1; int i 1; while (i n) { //Found fact * i; i; } System.out.println(n 的阶乘是 fact); // 预期输出120 } }解析阶乘计算需要遍历 1 到 n 的所有整数并相乘循环条件i n确保 i 从 1 累加到 5最终得到 5! 120。考题 5 答案javaimport java.util.Scanner; public class GuessNumber { public static void main(String[] args) { Scanner sc new Scanner(System.in); int target 7; // 目标数字 int guess; do { System.out.print(请猜一个1-10的数字); guess sc.nextInt(); if (guess target) { System.out.println(猜小了); } else if (guess target) { System.out.println(猜大了); } } while (guess ! target); //Found System.out.println(猜对了); sc.close(); } }解析do-while 循环需在猜对guess target时退出因此循环条件为guess ! target确保猜错时重复执行。考题 6 答案javapublic class PrintStar { public static void main(String[] args) { // 打印4行由*组成的直角三角形 for (int i 1; i 4; i) { for (int j 1; j i; j) { //Found System.out.print(*); } System.out.println(); } } }解析外层循环控制行数i1 到 4内层循环需打印与行数相同数量的*因此条件设为j i第 1 行 1 个 *第 2 行 2 个 *以此类推。