Case Study Practical Exercises | 案例分析实战演练

📚 Case Study Practical Exercises | 案例分析实战演练

In Key Stage 3 Computer Science, solving real-world problems through case studies helps you build computational thinking skills. This article walks through a complete practical exercise: designing a program that calculates average exam scores and assigns grades. You will learn how to analyse requirements, decompose problems, design algorithms, write pseudocode, and carry out testing — just like a professional developer.

在 KS3 计算机课程中,通过案例研究解决真实世界的问题有助于培养计算思维技能。本文将带你完成一个完整的实战练习:设计一个计算考试平均分并评定等级的程序。你将学习如何分析需求、分解问题、设计算法、编写伪代码并进行测试——就像专业的开发者一样。

1. Understanding the Problem | 理解问题

Our client, a class teacher, needs a simple digital tool. The program must accept a set of percentage scores from a recent exam, calculate the average, and then display the average together with a grade (A, B, C, D, or F). The grade boundaries are: 90–100 for A, 80–89 for B, 70–79 for C, 60–69 for D, and below 60 for F. The user should also be able to enter scores one by one until they type a negative number to finish.

我们的客户——一名班主任,需要一个简单的数字工具。程序必须接受一组最近考试的百分制分数,计算平均分,然后显示平均分以及一个等级(A、B、C、D 或 F)。等级边界为:90–100 为 A,80–89 为 B,70–79 为 C,60–69 为 D,60 以下为 F。用户还应能够逐个输入分数,直到输入一个负数来结束输入。

Before writing any code, we must be absolutely clear about the inputs, outputs, and processing rules. The input is a sequence of integers (the scores). The output is a single average value and its corresponding letter grade. If no valid scores are entered, the program should display an appropriate message instead of attempting to divide by zero.

在编写任何代码之前,我们必须完全清楚输入、输出和处理规则。输入是一系列整数(分数)。输出是一个平均值及其对应的字母等级。如果没有输入任何有效分数,程序应显示一条适当的信息,而不是尝试除以零。


2. Decomposition | 分解问题

Computational thinking begins with decomposition — breaking a complex problem into smaller, manageable sub-problems. For our score calculator, we can identify four distinct tasks: (1) collect score inputs until a sentinel value is given; (2) compute the arithmetic mean; (3) determine the grade using the boundary rules; (4) display the result clearly.

计算思维从分解开始——将一个复杂问题拆分为更小、更易于管理的子问题。对于我们的分数计算器,我们可以确定四个不同的任务:(1)收集分数输入,直到给出一个哨兵值;(2)计算算术平均值;(3)使用边界规则确定等级;(4)清晰地显示结果。

Each sub-problem can be solved independently and later combined. This modular approach makes it easier to code, test, and debug each part. For instance, if the grading logic has a mistake, we only need to look at the grade-determination function, not the whole program.

每个子问题都可以独立解决,然后再组合起来。这种模块化的方法使编码、测试和调试每个部分变得更加容易。例如,如果定级逻辑有误,我们只需查看等级判定函数,而不必检查整个程序。


3. Pattern Recognition | 模式识别

When examining the problem, we can spot familiar patterns. Computing an average is a classic ‘accumulator’ pattern: you keep a running total and a count, then divide at the end. The grading part follows a ‘decision tree’ pattern: a series of if–then–else checks that route the input to the correct outcome.

在审视问题时,我们可以发现熟悉的模式。计算平均值是一个经典的“累加器”模式:你维持一个运行总和与计数,最后再相除。等级评定部分遵循“决策树”模式:一系列如果–那么–否则的检查,将输入导向正确的结果。

Recognising these patterns saves time because we already know efficient ways to implement them. The accumulator pattern, for example, appears whenever we need totals or averages — in shopping carts, game scores, or temperature logs.

识别这些模式可以节省时间,因为我们已知高效实现它们的方法。例如,累加器模式出现在任何需要总计或平均值的地方——购物车、游戏积分或温度记录中都会用到。


4. Abstraction | 抽象

Abstraction is about hiding unnecessary detail and focusing on what really matters. In our case, we do not need to store individual student names, dates of birth, or the subject title. The essential information is just the set of numeric scores and the grading boundaries.

抽象在于隐藏不必要的细节,专注于真正重要的事情。在我们的案例中,不需要存储学生的姓名、出生日期或科目名称。关键信息只是数值分数的集合和等级边界。

We can model the problem with a simple mathematical formula for the average, and a function that maps a number to a grade. This abstraction allows us to write clean, reusable code that would work for any similar grading scenario, regardless of the class size or specific subject.

我们可以用一个简单的数学公式来对平均值建模,并用一个将数字映射到等级的函数来建模。这种抽象使我们能够编写简洁、可重用的代码,适用于任何类似的评分场景,无论班级人数或具体科目如何。


5. Algorithm Design – Flowchart | 算法设计 – 流程图

A flowchart provides a visual map of the algorithm. The process begins with a ‘Start’ oval. A parallelogram asks for a score. If the score is negative, the flow moves to a decision: has at least one valid score been entered? If yes, the average is calculated in a process box. If not, the flow goes to an output box showing ‘No scores entered’. After computing the average, we pass through a series of diamond-shaped decisions to assign the grade, finally ending at a ‘Display result’ parallelogram and an oval ‘End’.

流程图提供了算法的可视化地图。过程从一个“开始”椭圆形开始。一个平行四边形象征要求输入分数。如果分数为负,流程进入一个决策:是否至少输入了一个有效分数?如果是,则在处理框中计算平均值。如果不是,流程转至显示“未输入分数”的输出框。计算平均值后,我们通过一系列菱形决策来指定等级,最后到达一个“显示结果”平行四边形和一个椭圆形“结束”。

Drawing the flowchart before coding helps you spot logical errors early. For example, you can quickly see if the loop for input correctly handles the sentinel value and if all possible grade ranges are covered without gaps.

在编码前绘制流程图有助于尽早发现逻辑错误。例如,你可以快速查看输入循环是否正确处理了哨兵值,以及所有可能的等级范围是否都被涵盖而没有遗漏。


6. Algorithm Design – Pseudocode | 算法设计 – 伪代码

Pseudocode is a plain-English description of the steps, using programming-like structures. Here is one possible version for our case study:
START
SET total TO 0
SET count TO 0
OUTPUT “Enter score (negative to finish):”
INPUT score
WHILE score GREATER THAN OR EQUAL TO 0 DO
▯ total ← total + score
▯ count ← count + 1
▯ OUTPUT “Enter next score:”
▯ INPUT score
ENDWHILE
IF count IS 0 THEN
▯ OUTPUT “No valid scores entered.”
ELSE
▯ average ← total DIVIDED BY count
▯ IF average GREATER THAN OR EQUAL TO 90 THEN grade ← ‘A’
▯ ELSE IF average GREATER THAN OR EQUAL TO 80 THEN grade ← ‘B’
▯ ELSE IF average GREATER THAN OR EQUAL TO 70 THEN grade ← ‘C’
▯ ELSE IF average GREATER THAN OR EQUAL TO 60 THEN grade ← ‘D’
▯ ELSE grade ← ‘F’
▯ ENDIF
▯ OUTPUT “Average:”, average, “Grade:”, grade
ENDIF
STOP

伪代码是对步骤的简明英文描述,使用了类似编程语言的结构。以下是本案例的一种可能版本:
开始
将 total 设为 0
将 count 设为 0
输出 “请输入分数(负数结束):”
输入 score
当 score 大于或等于 0 时执行
▯ total ← total + score
▯ count ← count + 1
▯ 输出 “请输入下一个分数:”
▯ 输入 score
结束循环
如果 count 为 0 则
▯ 输出 “未输入有效分数。”
否则
▯ average ← total 除以 count
▯ 如果 average 大于或等于 90 则 grade ← ‘A’
▯ 否则如果 average 大于或等于 80 则 grade ← ‘B’
▯ 否则如果 average 大于或等于 70 则 grade ← ‘C’
▯ 否则如果 average 大于或等于 60 则 grade ← ‘D’
▯ 否则 grade ← ‘F’
▯ 结束如果
▯ 输出 “平均分:”, average, “等级:”, grade
结束如果
停止

Notice how the pseudocode uses indentation to show the structure clearly. The WHILE loop implements repeated input, and the nested IF–ELSE statements implement the decision tree for grading. This pseudocode can be directly translated into Python, Scratch, or any other language.

请注意伪代码如何使用缩进来清晰地表示结构。WHILE 循环实现了重复输入,而嵌套的 IF–ELSE 语句实现了等级评定的决策树。此伪代码可以直接翻译成 Python、Scratch 或任何其他语言。


7. Variables and Data Types | 变量与数据类型

Every piece of data in a program needs a variable with a suitable data type. In our algorithm we can identify these variables: score (integer, each input value), total (integer, accumulates scores), count (integer, counts valid entries), average (decimal/float, result of division), and grade (character/string, stores ‘A’ to ‘F’).

程序中的每份数据都需要一个带有合适数据类型的变量。在我们的算法中,我们可以识别出这些变量:score(整数,每个输入值)、total(整数,累加分数)、count(整数,有效条目计数)、average(小数/浮点数,除法结果),以及 grade(字符/字符串,存储 ‘A’ 至 ‘F’)。

Choosing the right types is important. If total and count are integers, dividing them in many languages gives an integer result unless we explicitly convert to float. The average must be a real number to show, for example, 85.5 correctly. The grade variable can be a single character, but a string works fine as well.

选择正确的类型很重要。如果 total 和 count 是整数,在许多语言中直接相除会得到整数结果,除非显式转换为浮点数。average 必须是实数,才能正确显示例如 85.5 这样的值。grade 变量可以是一个字符,但字符串也完全可以。


8. Sequence, Selection, Iteration | 顺序、选择、迭代

All programs are built from three fundamental control structures: sequence, selection, and iteration. Our case study uses all three. The overall flow is a sequence of steps: initialise, input loop, calculate, grade, output. The loop (WHILE) is an iteration that runs an unknown number of times until the sentinel appears. The grade assignment is a selection (IF–ELSE) that chooses one path from several.

所有程序都由三种基本控制结构构成:顺序、选择和迭代。我们的案例研究使用了全部三种结构。整体流程是一系列顺序步骤:初始化、输入循环、计算、评定等级、输出。循环(WHILE)是一种迭代,它运行未知次数,直到哨兵值出现。等级指定是一种选择(IF–ELSE),从多条路径中选择其中之一。

Understanding these structures helps you design any algorithm. Even a complex system like a video game can be broken down into sequences of instructions, repeated loops for animation frames, and conditional checks for collisions or health points.

理解这些结构有助于你设计任何算法。即使像视频游戏这样的复杂系统,也可以分解为指令序列、用于动画帧的重复循环,以及检测碰撞或生命值的条件检查。


9. Testing and Debugging | 测试与调试

No program works perfectly the first time. We need a test plan with normal, boundary, and erroneous data. Normal tests: enter scores 85, 92, 78 → average 85, grade B. Boundary tests: enter a single score of 90 → average 90, grade A; enter 59, 60, 61 → average 60, grade D. Erroneous test: entering no valid scores (just a negative number) should output a message, not crash.

任何程序都不可能在第一次就完美运行。我们需要一个包含正常、边界和错误数据的测试计划。正常测试:输入分数 85、92、78 → 平均分 85,等级 B。边界测试:输入单个分数 90 → 平均分 90,等级 A;输入 59、60、61 → 平均分 60,等级 D。错误测试:不输入任何有效分数(只输入一个负数)应输出提示信息,而不是崩溃。

A trace table can be used to follow the values of variables step by step, helping to find logic errors. For instance, if the loop condition is written as ‘score more than 0’ instead of ‘score >= 0’, a score of zero would be rejected incorrectly. Debugging often means checking such small but critical details.

可以使用跟踪表逐步追踪变量的值,帮助发现逻辑错误。例如,如果循环条件写成“score 大于 0”而不是“score >= 0”,则零分会被错误地拒绝。调试通常意味着检查这类细小但关键的细节。


10. Evaluation and Reflection | 评估与反思

After testing, we should evaluate whether the solution meets all requirements. The program correctly calculates an average and assigns a letter grade. It handles the empty-input case gracefully. However, we might notice that it does not validate whether scores are within 0–100. A user could type 150 and the average would be distorted. A future improvement would be to reject scores outside the valid range.

测试之后,我们应该评估解决方案是否满足所有需求。该程序能正确计算平均值并指定字母等级。它优雅地处理了空输入的情况。然而,我们可能会注意到它并未验证分数是否在 0–100 范围内。用户可能会输入 150,导致平均值失真。未来的改进是拒绝超出有效范围的分数。

Reflection also considers efficiency and readability. The current pseudocode is simple and clear enough for a KS3 assignment. If the program were used by hundreds of teachers, we might add a graphical interface or save results to a file — but the core logic would remain unchanged.

评估也涉及效率和可读性。当前的伪代码对于 KS3 作业来说足够简单清晰。如果该程序被数百名教师使用,我们可能会添加图形界面或将结果保存到文件中——但核心逻辑将保持不变。


11. Extending the Case Study | 案例延伸

Once the basic version works, we can extend it to deepen understanding. Possible extensions: allow the teacher to input multiple subjects and calculate an overall average; store student names alongside scores and output a rank list; use a weighting system where some exams count more (e.g., coursework 30%, final paper 70%). These extensions introduce arrays, records, and more complex formulas.

一旦基础版本能够运行,我们可以对其进行扩展以加深理解。可能的延伸包括:允许教师输入多个科目并计算总体平均分;将学生姓名与分数一起存储,并输出排名列表;使用权重系统,有些考试占更大比重(例如,课程作业占 30%,期末考试占 70%)。这些扩展引入了数组、记录和更复杂的公式。

For the weighted average, the formula becomes:

weighted_average = (score₁ × weight₁ + score₂ × weight₂) ÷ (weight₁ + weight₂)

This encourages you to think about how abstraction and decomposition apply to larger tasks.

对于加权平均,公式变为:

加权平均分 = (分数₁ × 权重₁ + 分数₂ × 权重₂) ÷ (权重₁ + 权重₂)

这促使你思考抽象和分解如何应用于更大的任务。


12. Conclusion | 结论

Case studies like this student grade calculator bring together every strand of the KS3 computing curriculum. You have practised problem decomposition, pattern spotting, abstraction, algorithm design in both flowchart and pseudocode forms, variable selection, and test planning. These are not just exam skills — they are real-world skills used by software engineers every day.

像这个学生成绩计算器这样的案例研究汇集了 KS3 计算机课程的各个方面。你练习了问题分解、模式发现、抽象、以流程图和伪代码形式进行的算法设计、变量选择以及测试计划。这些不仅是考试技能——它们也是软件工程师日常使用的真实世界技能。

Remember that the key to mastering case studies is deliberate practice. Try adapting the pseudocode into a different programming language, or invent your own case study, such as a library book loan tracker or a weather data analyser. Each exercise will strengthen your computational thinking and make you a more confident programmer.

请记住,掌握案例研究的关键在于刻意练习。尝试将伪代码改编成另一种编程语言,或者设计你自己的案例,如图书馆借阅追踪器或天气数据分析器。每个练习都会增强你的计算思维,让你成为更自信的程序员。


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

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

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version