📚 Case Study in Action: Building a Maths Quiz | 案例分析实战:制作数学问答游戏
In Year 7 computing, you will often be asked to solve a problem by following a series of logical steps. A case study is a fantastic way to bring together all the key skills you have learned — from breaking down a problem and designing an algorithm to writing code and testing it. In this article, we will work through a complete case study: creating a simple maths quiz that asks five random multiplication questions. You will see how computational thinking helps you plan, build and improve a piece of software just like a real developer.
在七年级计算机课程中,你经常需要按照一系列逻辑步骤来解决问题。案例研究是将你所学到的所有关键技能 — 从分解问题、设计算法到编写代码和测试 — 汇集到一起的绝佳方式。在本文中,我们将完整演练一个案例:制作一个简单的数学问答游戏,它会随机提出五个乘法问题。你将看到计算思维如何帮助你像真正的开发者一样规划、构建和改进软件。
1. Introduction to Case Studies | 案例研究简介
A case study is a detailed investigation of a real‑world scenario. In computing, it usually means analysing a problem, designing a solution, implementing it and then evaluating the outcome. By working through a case study, you practise thinking like a computer scientist. You learn to ask: “What exactly needs to be built? What are the inputs and outputs? How can I test my solution?”
案例研究是对真实场景的详细调查。在计算机科学中,它通常意味着分析问题、设计解决方案、实现方案然后评估结果。通过完成一个案例研究,你可以练习像计算机科学家一样思考。你要学会问自己:“到底需要构建什么?输入和输出是什么?我怎样才能测试我的解决方案?”
2. Understanding the Problem | 理解问题
Our task is to write a program that gives the user five random multiplication questions. The two numbers in each question must be between 1 and 10. After the user types an answer, the program should say whether it is correct or wrong and keep a running score. At the end, the total score out of 5 should be displayed along with a friendly message.
我们的任务是编写一个程序,向用户提出五个随机的乘法问题。每个问题中的两个数字必须在 1 到 10 之间。用户输入答案后,程序应显示回答正确还是错误,并记录分数。最后,应显示满分 5 分中的总得分以及一条友好的消息。
Before writing any code, we must be absolutely clear about the requirements. If we miss a detail, our final program will not meet the user’s needs. In a real case study, you would often write a short list of success criteria. For this project, the criteria are: generate random numbers, ask exactly 5 questions, check answers, keep score and display a final result.
在编写任何代码之前,我们必须完全清楚需求。如果我们遗漏了一个细节,最终的程序就无法满足用户的需要。在真实的案例研究中,你通常会写一份简短的成功标准清单。对于这个项目,标准是:生成随机数、提出恰好 5 个问题、检查答案、记录分数并显示最终结果。
3. Breaking Down the Problem | 分解问题
Decomposition means breaking a large problem into smaller, manageable parts. For our maths quiz, we can split the job into these sub‑tasks: initialise the score to zero, repeat a block of instructions five times, generate two random numbers, display the question, accept the user’s answer, compare the answer with the correct product, update the score and give feedback, and finally display the total score.
分解意味着将一个大问题拆分成更小、更容易处理的部分。对于我们的数学问答游戏,可以将工作分解为以下几个子任务:将分数初始化为零、重复执行一个指令块五次、生成两个随机数、显示问题、接受用户的答案、将答案与正确的乘积进行比较、更新分数并提供反馈,最后显示总得分。
This list looks a bit like the steps of a recipe. Once you have written down all the sub‑tasks, you can tackle them one by one. Decomposition also helps you spot which parts you already know how to code and which parts you need to research. For example, generating a random number might be new, but asking for input is probably familiar.
这份清单看起来有点像食谱的步骤。一旦你把所有子任务写下来,就可以逐一处理它们。分解还能帮助你发现哪些部分你已经知道如何编写代码,哪些部分需要进一步研究。例如,生成随机数可能是新内容,但请求输入可能已经很熟悉了。
4. Designing the Algorithm with Flowcharts | 使用流程图设计算法
An algorithm is a step-by-step plan for solving a problem. Before coding, it is wise to design your algorithm using a flowchart or pseudocode. A flowchart uses standard symbols: an oval for start and end, a rectangle for a process, a diamond for a decision and a parallelogram for input/output. Here is a simplified version of our quiz algorithm in words:
算法是解决问题的分步骤计划。在编码之前,明智的做法是使用流程图或伪代码来设计算法。流程图使用标准符号:椭圆形表示开始和结束,矩形表示处理过程,菱形表示判断,平行四边形表示输入/输出。以下是我们问答游戏算法的简化文字版本:
Start → Set score to 0 → Repeat 5 times:
Generate number1 (1–10) → Generate number2 (1–10) →
Display “What is number1 × number2?” → Input userAnswer →
If userAnswer = number1 × number2 then say “Correct” and add 1 to score
Else say “Wrong, the answer is …” →
End repeat → Display “Your score is score out of 5” → Stop
开始 → 将分数设为 0 → 重复 5 次:
生成数字1(1–10) → 生成数字2(1–10) →
显示 “数字1 × 数字2 等于多少?” → 输入 userAnswer →
如果 userAnswer = 数字1 × 数字2,则说“正确”并把分数加 1
否则说“错误,答案是 …” →
结束重复 → 显示 “你的分数是 分数 分(满分5分)” → 停止
Even this simple plan makes the coding stage much easier because you no longer need to think about what to do next — you just follow the steps you have designed.
即使是这样一个简单的计划,也能让编码阶段变得更容易,因为你不再需要思考下一步该做什么 — 只需按照你设计的步骤执行即可。
5. Variables and Data Structures | 变量与数据结构
In this program we will need several variables. A variable is a named memory location that stores a value which can change while the program runs. We need a variable to hold the current score, which starts at 0 and increases by 1 for each correct answer. We also need variables for the two random numbers, for the user’s answer and perhaps a counter to keep track of how many questions have been asked.
在这个程序中,我们需要几个变量。变量是一个命名的内存位置,它存储一个在程序运行期间可以改变的值。我们需要一个变量来保存当前分数,它从 0 开始,每答对一题就增加 1。我们还需要变量来保存两个随机数、用户的答案,或许还需要一个计数器来跟踪已经问了多少个问题。
In many programming languages we choose a variable type. Here, all our variables can be integers (whole numbers). The counter could be an integer that starts at 1 and goes up to 5, or we can use a for‑loop that automatically handles counting. If we wanted to store all the questions and answers, we might use a list, but for this simple quiz single variables are enough.
在许多编程语言中,我们会选择一个变量类型。这里,我们所有的变量都可以是整数(整数)。计数器可以是一个从 1 到 5 的整数,或者我们可以使用 for 循环自动处理计数。如果我们想存储所有的问题和答案,可能会使用列表,但对于这个简单的问答游戏,单个变量就足够了。
6. Making Decisions with Conditionals | 使用条件语句做决策
A program often needs to take different paths based on conditions. In our quiz, after the user enters an answer, we must decide whether it matches the correct answer. We use an if‑else statement: IF the user’s answer equals number1 multiplied by number2, THEN display a congratulations message and increase the score. ELSE, display a message that shows the correct answer.
程序通常需要根据条件选择不同的路径。在我们的问答游戏中,用户输入答案后,我们必须判断它是否与正确答案匹配。我们使用 if‑else 语句:如果用户的答案等于数字1乘以数字2,那么显示一条祝贺消息并增加分数;否则,显示一条包含正确答案的消息。
Conditionals are one of the most powerful tools in programming because they allow software to react intelligently to different inputs. Without them, every run of the program would be identical. In Scratch, you would use an “if … then … else” block. In Python, the code might look like this:
条件语句是编程中最强大的工具之一,因为它让软件能够智能地对不同的输入做出反应。没有条件语句,程序每次运行都会完全相同。在 Scratch 中,你会使用“如果 … 那么 … 否则”积木块。在 Python 中,代码可能如下所示:
if userAnswer == num1 * num2:
print("Correct!")
score = score + 1
else:
print("Wrong. The answer is", num1 * num2)
Notice the double equals sign “==” is used to assign a value. This is a common point of confusion for beginners, so remember: “==” asks the question “Are these equal?”, and “=” stores a value.
请注意双等号“==”用于检查是否相等,而单等号“=”用于赋值。这是初学者容易混淆的一点,所以请记住:“==”问的是“这两个相等吗?”,而“=”用来存储一个值。
7. Repeating Actions with Loops | 使用循环重复操作
We need to ask exactly five questions. Rather than writing the same code five times, we use a loop. A loop repeats a block of code a set number of times or until a condition is met. For this case study, a count‑controlled loop is ideal: we know in advance that we want exactly five repetitions.
我们需要问恰好五个问题。与其把相同的代码写五遍,我们使用循环。循环会重复执行一个代码块设定好的次数,或者直到满足某个条件为止。对于这个案例研究,计数控制的循环是理想选择:我们预先知道想要正好五次重复。
In pseudocode we might write: FOR questionNumber FROM 1 TO 5 DO … END FOR. In Python, a very common pattern is:
for i in range(5):
# ask a question and check answer
In Scratch, you would use a “repeat 5” block. Inside the loop we place all the steps for one question: generating the numbers, asking the question, checking the answer and updating the score. The loop ensures these steps happen five times, once for each question.
在 Scratch 中,你会使用“重复执行 5 次”积木块。在循环内部,我们放置一道题的所有步骤:生成数字、提出问题、检查答案并更新分数。循环确保这些步骤发生五次,每道题一次。
8. Handling User Input and Output | 处理用户输入与输出
For the quiz to work, our program must be able to talk to the user. Output means displaying information on the screen, such as the question and the final score. Input means receiving data from the user, usually via the keyboard. Because the user’s input is normally read as text, we must convert it to a number before comparing it with the mathematical answer.
为了让问答游戏正常运行,我们的程序必须能够与用户交流。输出意味着在屏幕上显示信息,例如问题和最终分数。输入意味着从用户那里接收数据,通常是通过键盘。由于用户的输入通常被读取为文本,我们必须将其转换为数字,然后才能与数学答案进行比较。
In Python we use the input() function to ask a question and receive the reply, and int() to convert that text reply into an integer. For example: userAnswer = int(input("What is 3 × 7? ")). If you forget to convert the input, the program might crash or give strange results when it tries to compare a string with a number. Always think carefully about data types.
在 Python 中,我们使用 input() 函数来提出问题并接收回答,并使用 int() 将文本回复转换为整数。例如:userAnswer = int(input("3 × 7 等于多少?"))。如果你忘记转换输入,当程序试图将字符串与数字进行比较时,可能会崩溃或给出奇怪的结果。一定要仔细考虑数据类型。
9. Testing and Debugging | 测试与调试
No code works perfectly the first time. Testing is the process of running the program with different inputs to make sure it behaves as expected. We should test normal cases, such as giving a correct answer, and edge cases, such as typing a letter instead of a number. A good tester also plans what the expected outcome should be before running each test.
没有代码第一次就能完美运行。测试是用不同的输入运行程序以确保其行为符合预期的过程。我们应该测试正常情况,例如给出一个正确答案,以及边缘情况,例如输入字母而不是数字。一个好的测试员还会在运行每个测试之前,计划好预期的结果应该是什么。
Here is a simple test plan for our quiz:
| Test Case (English) | 测试用例 | Input | Expected Result |
|---|---|---|---|
| Correct answer | 正确答案 | 42 (when 6×7) | "Correct", score +1 |
| Wrong answer | 错误答案 | 99 (when 6×7) | "Wrong", score unchanged |
| Non‑numeric input | 非数字输入 | "hello" | Error message / handle gracefully |
| Boundary: 0 as answer | 边界:回答 0 | 0 | Treated as wrong (unless product is 0) |
Debugging is fixing the errors you find. Common bugs in this project might be forgetting to reset the score, not generating new random numbers inside the loop, or using the wrong comparison operator. Always read your code carefully and test small sections at a time.
调试是修复发现的错误。在这个项目中常见的错误可能包括忘记重置分数、没有在循环内部生成新的随机数,或者使用了错误的比较运算符。一定要仔细阅读代码,并一次测试一小部分。
10. Evaluation and Improvement | 评估与改进
Once your program runs correctly, it is time to evaluate it against the original success criteria. Does it ask exactly five questions? Are the numbers truly random and between 1 and 10? Is the score displayed correctly? If the answer is yes to all these, you have met the requirements. But a good case study also asks: “How could I make this even better?”
当你的程序正确运行后,就该根据最初的成功标准对其进行评估了。它是否恰好问了五个问题?数字是否真正随机且在 1 到 10 之间?分数是否正确显示?如果所有这些问题的答案都是肯定的,那么你就满足了需求。但一个好的案例研究还会问:“我怎样才能让它变得更好?”
Possible improvements include adding a difficulty level that changes the range of numbers, letting the user choose how many questions to answer, keeping a high‑score table, or adding a timer. Even a small change forces you to think about how your algorithm and code structure would need to adapt — this is excellent practice for future projects.
可能的改进包括:增加一个改变数字范围的难度级别,让用户选择回答多少道题,保留一个最高分排行榜,或者添加一个计时器。即使是一个小小的改动,也会迫使你思考算法和代码结构应该如何调整 — 这对未来的项目来说是绝佳的练习。
11. Real-World Connections | 现实世界联系
You might think a simple maths quiz is just a classroom exercise, but the same principles power huge systems. Every time you use a learning app that gives you random questions, the programmers used decomposition, loops and conditionals just like we did. Online testing platforms, language‑learning apps and even professional certification exams rely on algorithms that present random tasks and track scores.
你可能会觉得一个简单的数学问答游戏只是课堂练习,但同样的原理支撑着庞大的系统。每当你使用一个为你提供随机问题的学习应用程序时,程序员都使用了分解、循环和条件语句,就像我们做的那样。在线测试平台、语言学习应用程序,甚至专业认证考试,都依赖于那些呈现随机任务并跟踪分数的算法。
Moreover, the ability to write a clear algorithm and test it thoroughly is a skill that professional software engineers use every day. Whether they are building a banking system or a video game, the cycle of analysis, design, implementation and evaluation — exactly the case study approach — is at the heart of their work.
此外,写出清晰的算法并对其进行全面测试的能力,是专业软件工程师每天都在使用的技能。无论他们是在构建银行系统还是视频游戏,分析、设计、实现和评估的循环 — 正是案例研究的方法 — 是他们工作的核心。
12. Key Takeaways | 关键要点
Let’s review the main computing concepts you have practised in this case study:
• Decomposition – breaking a problem into smaller parts makes it easier to solve.
• Algorithms – a step‑by‑step plan, often drawn as a flowchart, guides your coding.
• Variables – store data that changes, like the score.
• Conditionals – IF statements let the program make decisions.
• Loops – repeat instructions efficiently without duplicating code.
• Testing and debugging – essential steps to catch mistakes and improve the program.
• Evaluation – reflecting on whether you met the goals and thinking of improvements.
让我们回顾一下你在这个案例研究中所练习的主要计算概念:
• 分解 — 把问题拆分成更小的部分,使其更容易解决。
• 算法 — 一个分步骤的计划,通常绘制成流程图,指导你的编码。
• 变量 — 存储会变化的数据,比如分数。
• 条件语句 — IF 语句让程序能够做出决策。
• 循环 — 高效地重复指令,无需复制代码。
• 测试与调试 — 发现错误并改进程序的关键步骤。
• 评估 — 反思你是否达成了目标,并思考改进之处。
By completing this case study from start to finish, you have walked through the very same process that computer scientists use to solve real problems. Keep practising with your own ideas — every new project strengthens your computational thinking skills.
通过从头到尾完成这个案例研究,你经历了计算机科学家用来解决真实问题的完全相同的过程。用自己的想法不断练习吧 — 每个新项目都会增强你的计算思维能力。
Published by TutorHao | Computing Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图