← 返回资讯屏轩教育 · 行业资讯

Year 9 CAIE Computer Science: Case Study Practical Exercise | Year 9 CAIE 计算机:案例分析实战演练

2026-07-19 03:17 · 本文部分内容由AI辅助创作

📚 Year 9 CAIE Computer Science: Case Study Practical Exercise | Year 9 CAIE 计算机:案例分析实战演练

Welcome to this step-by-step case study walkthrough for Year 9 CAIE Computer Science. Throughout this article, you will apply computational thinking and problem-solving techniques to a real programming scenario: building a 'Guess the Number' game. By working through each stage — from understanding requirements to testing and evaluation — you will strengthen the skills needed for coursework and examinations.

欢迎来到这篇为 Year 9 CAIE 计算机量身打造的案例分析实战指南。我们将逐步运用计算思维和问题解决方法,完成一个真实的编程情境:构建“猜数字”游戏。从理解需求到测试与评估,每个阶段都将帮助你巩固课程和考试所需的技能。

1. Understanding the Case Study Scenario | 理解案例情境

A small educational technology company wants to develop an interactive game that helps younger students practise logical reasoning and number sense. The proposed game will generate a secret number between 1 and 100, then repeatedly ask the player to guess. After each guess, the program should respond with 'Too high', 'Too low', or 'Correct'. To add a challenge, it will also track the number of attempts and display them when the player wins. The game should allow the player to exit by entering 0 at any time.

一家小型教育科技公司希望开发一个互动游戏,帮助低年级学生练习逻辑推理和数感。游戏将生成一个1到100之间的秘密数字,然后反复要求玩家猜测。每次猜测后,程序应回应“太高”、“太低”或“正确”。为增加挑战性,它还会记录猜测次数并在玩家获胜时显示。任何时候玩家输入0,游戏即退出。

The scenario is typical for introducing fundamental programming concepts: variables, input/output, conditional statements, loops, random number generation and input validation. Our task is to analyse, design, implement and evaluate a solution using computational thinking.

这种情境非常适合引入基础编程概念:变量、输入/输出、条件语句、循环、随机数生成和输入验证。我们的任务是运用计算思维进行分析、设计、实现和评估一个解决方案。


2. Problem Definition and Requirements | 问题定义与需求

Before writing any code, it is crucial to define the problem precisely. We can break down the requirements into functional and non-functional specifications. Functional requirements describe what the software must do, while non-functional requirements describe how it should behave, including performance, usability and constraints.

在编写任何代码之前,精确地定义问题至关重要。我们可以将需求分解为功能性需求和非功能性需求。功能性需求描述软件必须做什么,而非功能性需求描述软件的表现方式,包括性能、可用性和约束条件。

Functional Requirements中文:功能性需求
Generate a random integer between 1 and 100.生成一个1到100之间的随机整数。
Prompt the user to enter a guess.提示用户输入猜测数字。
Compare the guess with the secret number and display 'Too high', 'Too low' or 'Correct'.将猜测与秘密数字进行比较,显示“太高”、“太低”或“正确”。
Count the number of attempts and show the total when the game is won.计算猜测次数,并在获胜时显示总次数。
Terminate the program if the user enters 0.如果用户输入0,则终止程序。

Non-functional requirements include: the program must respond instantly, be easy to understand, and handle incorrect inputs gracefully (e.g., letters or numbers outside the range). Identifying these requirements early ensures we design a solution that meets both explicit and implicit expectations.

非功能性需求包括:程序必须即时响应、易于理解,并能优雅地处理错误输入(例如字母或超出范围的数字)。尽早识别这些需求可确保我们的设计满足明确的和隐含的期望。


3. Algorithm Planning: Use of Pseudocode | 算法规划:伪代码的运用

An algorithm is a step-by-step procedure for solving a problem. Before coding, we can express the solution in pseudocode — a language-independent, readable description of the logic. Our pseudocode will capture the sequence, selection, and repetition needed for the game.

算法是解决问题的分步过程。在编码之前,我们可以用伪代码来表达解决方案,伪代码是一种独立于语言的、可读的逻辑描述。我们的伪代码将捕捉游戏所需的顺序、选择和循环结构。

The core logic can be written as follows (using a simple pseudocode style):

核心逻辑可以写成如下形式(采用简单的伪代码风格):

SET secretNumber ← RANDOM(1, 100)
SET attempts ← 0
REPEAT
OUTPUT "Enter your guess (0 to quit):"
INPUT guess
IF guess = 0 THEN
OUTPUT "Game exited."
EXIT LOOP
ENDIF
attempts ← attempts + 1
IF guess < secretNumber THEN
OUTPUT "Too low"
ELSE IF guess > secretNumber THEN
OUTPUT "Too high"
ELSE
OUTPUT "Correct! You guessed it in ", attempts, " tries."
EXIT LOOP
ENDIF
END REPEAT

设定 secretNumber ← 随机数(1, 100)
设定 attempts ← 0
重复
输出 "输入你的猜测(按 0 退出):"
输入 guess
如果 guess = 0 那么
输出 "游戏已退出。"
退出循环
结束如果
attempts ← attempts + 1
如果 guess < secretNumber 那么
输出 "太低"
否则如果 guess > secretNumber 那么
输出 "太高"
否则
输出 "正确!你猜了 ", attempts, " 次。"
退出循环
结束如果
结束重复

This pseudocode clearly shows the sequence of actions (prompt, input, compare, output) and the conditional branching. It also demonstrates a REPEAT loop that continues indefinitely until either a correct guess or a quit command is received. The use of indentation enhances readability and mirrors actual coding structures.

这段伪代码清晰地展示了动作顺序(提示、输入、比较、输出)和条件分支。它还演示了一个 REPEAT 循环,该循环会无限继续,直到收到正确猜测或退出指令。缩进的使用增强了可读性,并与实际编码结构相呼应。


4. Designing a Flowchart | 设计流程图

A flowchart is a visual representation of an algorithm, using standard symbols. It helps programmers and stakeholders understand the flow of control at a glance. For our number guessing game, we can map the logic using:

流程图使用标准符号直观地呈现算法,帮助程序员和利益相关者一目了然地理解控制流程。对于我们的猜数字游戏,可以使用以下符号进行映射:

椭圆形:开始/结束(终止符)

平行四边形:输入/输出

矩形:处理(例如生成随机数、猜测次数递增)

菱形:判断(猜测是否为 0?猜测是否小于秘密数字?)

箭头:控制流向

The flowchart would start with a process box generating the secret number and initialising attempts. Then a parallelogram asks for user input. A diamond checks if input equals 0; if true, it goes to an end terminal. Otherwise, it increments attempts, then another diamond checks if the guess equals the secret number. Depending on the outcome, it displays a message and either ends or loops back. Building a flowchart forces you to consider every possible path, making it an essential planning tool before coding.

流程图会从一个处理框生成秘密数字并初始化猜测次数开始。然后一个平行四边形要求用户输入。一个菱形判断输入是否等于 0;若是,则流向结束终端。否则,猜测次数递增,然后另一个菱形判断猜测是否等于秘密数字。根据结果,显示信息并结束或循环回去。构建流程图迫使你考虑所有可能的路径,因此在编码前是一项必不可少的规划工具。


5. Decomposing the Problem into Modules | 将问题分解为模块

Decomposition, or breaking a complex problem into smaller, manageable parts, is a core computational thinking skill. The guessing game can be divided into several modules:

分解,即将复杂问题拆分为更小、更易管理的部分,是一项核心计算思维技能。猜数字游戏可以分为几个模块:

Module 1 – Initialisation: generate the secret number and set attempts to zero.

模块 1 – 初始化:生成秘密数字并将猜测次数设为零。

Module 2 – User input: prompt the user and read a value, including validation.

模块 2 – 用户输入:提示用户并读取一个值,包括验证。

Module 3 – Comparison and feedback: check the guess against the secret number and produce the appropriate message.

模块 3 – 比较与反馈:将猜测与秘密数字比较,生成相应信息。

Module 4 – Termination: handle win condition or quit command.

模块 4 – 终止:处理获胜条件或退出命令。

This modular approach makes the program easier to develop, test and debug. Each module can be implemented and tested independently before integration, reducing complexity and the chance of errors.

这种模块化方法使程序更易于开发、测试和调试。每个模块都可以在集成前独立实现和测试,从而降低复杂性并减少出错几率。


6. Variable Declaration and Data Types | 变量声明与数据类型

Choosing the right variables and data types is fundamental. In our pseudocode and eventual code, we need:

选择正确的变量和数据类型是基础。在我们的伪代码和最终代码中,需要:

VariableData TypePurpose中文说明
secretNumberintegerStores the random target保存随机目标数字
guessintegerHolds the user's guess保存用户猜测
attemptsintegerCounts the number of guesses记录猜测次数

Using integer types for all three variables ensures the program can store whole numbers correctly. Many programming languages require you to declare variables before use, and choosing a descriptive name (such as userGuess rather than g) improves readability.

所有三个变量都使用整数类型可确保程序正确存储整数。许多编程语言要求在使用前声明变量,而选择描述性名称(例如用 userGuess 而非 g)可提高可读性。

Moreover, attention to data type prevents errors. For example, if the random function returns a floating-point number, we must convert it to an integer, otherwise comparisons may behave unexpectedly. Validating the data type of user input is equally important — we must ensure the input is numeric before using it.

此外,关注数据类型可以防止错误。例如,若随机函数返回浮点数,我们必须将其转换为整数,否则比较时可能出现意外行为。验证用户输入的数据类型同样重要——我们必须确保输入是数值后再使用。


广告 / Sponsored

7. Implementing Conditional Statements | 条件语句的实现

Conditional (selection) statements allow the program to make decisions. In the guessing game, we need to check three conditions: guess equals 0, guess less than secretNumber, and guess greater than secretNumber. A typical implementation uses IF–ELSE IF–ELSE constructs.

条件(选择)语句允许程序做出决策。在猜数字游戏中,我们需要检查三个条件:猜测等于 0,猜测小于 secretNumber,以及猜测大于 secretNumber。典型的实现使用 IF–ELSE IF–ELSE 结构。

In pseudocode: IF condition THEN do something ELSE IF another condition THEN do something ELSE do something ENDIF. In many languages, this translates directly — for example in Python: if guess == 0: ... elif guess < secretNumber: ... else: ...

伪代码中:IF 条件 THEN 执行操作 ELSE IF 另一条件 THEN 执行操作 ELSE 执行操作 ENDIF。在许多语言中,这可以直接转换——例如 Python 中:if guess == 0: ... elif guess < secretNumber: ... else: ...

Order matters: because we check for quit (0) first, the program exits before counting an invalid guess. Nesting conditions inside a loop allows repeated decision-making until the exit condition is met. Understanding logical operators like AND, OR and NOT is also valuable when combining conditions, although for this case they are not necessary.

顺序很重要:因为我们首先检查退出(0),程序会在计数之前退出。将条件嵌套在循环中可以在满足退出条件之前反复进行决策。理解 AND、OR 和 NOT 等逻辑运算符在组合条件时也很有价值,尽管在此案例中并非必需。


8. Loop Structures in Practice | 循环结构的实践

Loops are essential for repeating a block of code. The guessing game requires a loop that continues until the user guesses correctly or enters 0. In pseudocode we used a REPEAT-UNTIL-equivalent, but most languages provide WHILE or FOR loops.

循环对于重复执行代码块至关重要。猜数字游戏需要一个循环,直到用户猜对或输入 0 结束。伪代码中我们使用了类似 REPEAT-UNTIL 的结构,但大多数语言提供 WHILE 或 FOR 循环。

A pre-condition loop (WHILE) checks the condition before executing the loop body. A post-condition loop (DO...WHILE) executes the body at least once before checking. Our game is a good fit for a post-condition loop because we need to prompt the user at least once. However, by setting a flag variable (e.g., playing ← TRUE) we can simulate the same logic with a WHILE loop that continues as long as playing is true.

先判断循环(WHILE)在执行循环体之前检查条件。后判断循环(DO...WHILE)在执行循环体至少一次后再检查。我们的游戏非常适合后判断循环,因为我们至少需要提示用户一次。但是,通过设置标志变量(例如 playing ← TRUE),我们可以用 WHILE 循环模拟相同逻辑,只要 playing 为真就继续。

In digital logic, an infinite loop could crash the program; hence we must ensure there is a definite exit path. Our algorithm provides two clear exit paths: a correct guess or the user quitting. Trace the loop carefully to confirm it cannot get stuck.

在数字逻辑中,无限循环可能导致程序崩溃;因此我们必须确保有明确的退出路径。我们的算法提供了两种清晰的退出路径:猜对或用户退出。仔细追踪循环,确保它不会卡住。


9. Error Handling and Input Validation | 错误处理与输入验证

Robust programs anticipate invalid inputs and handle them gracefully. In our game, a user might enter a letter, a decimal, or a number outside 1–100. Without validation, the program could crash or produce meaningless output.

健壮的程序会预测无效输入并妥善处理。在我们的游戏中,用户可能输入字母、小数或超出 1–100 的数字。如果不验证,程序可能崩溃或产生无意义的输出。

A defensive strategy involves: first, checking whether the input can be converted to an integer. If not, display an error message and prompt again without incrementing the attempt counter. Second, if the input is an integer but not 0 and not between 1 and 100, inform the user and ask again. This layer of validation ensures the core game logic only ever receives valid data.

防御性策略包括:首先,检查输入是否可以转换为整数。如果不能,显示错误信息并重新提示,同时不增加猜测计数器。其次,如果输入是整数但既不是 0 也不在 1–100 之间,告知用户并再次询问。此验证层确保核心游戏逻辑只接收有效数据。

In pseudocode, this can be represented with an inner loop that keeps asking until a valid input is provided. Adding error messages like "Invalid input – please enter a whole number between 1 and 100" improves the user experience significantly.

在伪代码中,可以用一个内层循环来表示,该循环持续询问直到提供有效输入。添加诸如“输入无效 – 请输入 1 到 100 之间的整数”的错误信息,可以显著提升用户体验。


10. Testing Strategy and Trace Tables | 测试策略与追踪表

Testing is a systematic process to verify that the program works as intended. A trace table is a tool used to dry-run an algorithm by recording the values of variables at each step. It helps identify logic errors without needing a computer.

测试是验证程序是否按预期工作的系统性过程。追踪表是一种通过记录每一步变量值来模拟运行算法的工具,有助于在不使用计算机的情况下发现逻辑错误。

Let's dry-run a scenario where the secret number is 42 and the user enters 50, then 30, then 42. The trace table might look like this:

假设秘密数字是 42,用户依次输入 50、30、42,我们来模拟运行一下。追踪表可能如下:

StepsecretNumberguessattemptsOutput中文说明
142-0-初始化
242501"Too high"太高
342302"Too low"太低
442423"Correct! 3 tries"正确!3次

Once the trace shows correct behaviour, we design test cases covering normal, boundary, and erroneous data. Boundary values such as 1 and 100, the quit command 0, and non-integer inputs should all be tested. A well-planned test strategy builds confidence in the program's reliability.

追踪显示正确行为后,我们设计涵盖正常数据、边界数据和错误数据的测试用例。边界值如 1 和 100、退出命令 0 以及非整数输入都应进行测试。精心规划的测试策略可以增强程序可靠性的信心。


11. Debugging Common Mistakes | 调试常见错误

Even with careful planning, errors occur. A logical error could cause the program to always say "Too high" because of a reversed comparison operator. In our case, using '>' instead of '<' would produce incorrect feedback. Another bug might be incrementing the attempt counter even when the user enters 0 to quit, which skews the statistics.

即使规划周密,错误仍会发生。一个逻辑错误可能导致程序总是显示“太高”,因为比较运算符颠倒了。在我们的案例中,使用“>”而非“<”将产生错误反馈。另一个可能的 bug 是即使用户输入 0 退出时仍增加猜测计数器,这会使统计数据失真。

Debugging techniques include: inserting temporary output statements to display variable values at key points (often called 'print debugging'), stepping through the code line by line using a debugger tool, or simply re-reading the pseudocode and comparing it with the actual code. An effective method for beginners is to explain the code aloud to a peer or a rubber duck — this often reveals hidden flaws.

调试技巧包括:插入临时输出语句在关键点显示变量值(通常称为“打印调试”),使用调试工具逐行单步执行代码,或者重新阅读伪代码并与实际代码进行比较。对初学者来说,一种有效的方法是向同伴或一只橡皮鸭大声解释代码,这常常能揭示隐藏的缺陷。

Keep a debugging log where you note down what the error was, how you detected it, and the fix applied. This record becomes a valuable learning resource and demonstrates a reflective approach required for CAIE assessments.

请保留一份调试日志,记录错误是什么、如何发现的以及应用的修复方法。这份记录将成为宝贵的学习资源,并展示出 CAIE 评估所要求的反思性方法。


12. Evaluation and Reflection | 评估与反思

After completing the game, it is important to evaluate the final solution against the original requirements. Did the program generate a random number between 1 and 100? Did it correctly count attempts and only display them upon winning? Did it terminate on input 0 without incrementing the counter? A thorough evaluation checks every functional and non-functional requirement.

完成游戏后,对照最初需求评估最终解决方案很重要。程序是否生成了 1 到 100 之间的随机数?是否正确计数并在获胜时显示?是否在输入 0 时终止且未增加计数器?全面的评估会检查每一项功能性和非功能性需求。

Beyond correctness, consider efficiency and user experience. Could the feedback be enhanced with more friendly messages? Could the maximum number of attempts be limited to make the game harder? Reflection helps identify improvements for a second iteration. For example, you might add a feature: a random hint after every five incorrect guesses. Documenting these ideas shows the ability to think critically about your own work.

除了正确性,还要考虑效率和用户体验。反馈信息是否可以用更友好的消息来增强?是否可以限制最大猜测次数以增加游戏难度?反思有助于确定第二

Published by TutorHao | Year 9 Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao