📚 Case Study Practice: Number List Analyser | 案例分析实战演练:数字列表分析器
In this article, we will work through a complete case study suitable for Year 7 Computer Science. The task is to design and understand a program called the ‘Number List Analyser’, which reads a sequence of integers from the user, terminated by zero, and then outputs the count of positive numbers, negative numbers, zeros, and the average value. This hands-on example will help you practise problem decomposition, algorithm design, flowchart construction, pseudocode writing, and testing strategies.
本文将带你完成一个适合七年级计算机科学的完整案例分析。任务是设计并理解一个名为“数字列表分析器”的程序,该程序从用户处读取一系列整数(以零结束),然后输出正数、负数、零的个数以及平均值。这个动手实例将帮助你练习问题分解、算法设计、流程图构建、伪代码编写和测试策略。
1. Introduction to the Case Study | 案例介绍
The Number List Analyser is a simple command-line program. A user enters integers one by one. The program keeps reading until a sentinel value of 0 is entered. Once the input stops, the program calculates and displays: total numbers entered (excluding the terminating zero), how many were positive, how many were negative, how many zeros (if any zeros were entered before the final zero), and the arithmetic mean of all the numbers (sum divided by count). If the first number entered is 0, the program should report that no valid data was provided.
数字列表分析器是一个简单的命令行程序。用户逐个输入整数。程序不断读取,直到输入哨兵值0为止。输入停止后,程序计算并显示:输入的数字总数(不包括结束的零)、正数的数量、负数的数量、零的数量(如果在结束零之前输入了零),以及所有数字的算术平均值(总和除以数量)。如果输入的第一个数字就是0,程序应报告没有提供有效数据。
2. Understanding the Problem | 理解问题
Before writing any code, we must fully understand what the program is supposed to do. The inputs are integers entered via the keyboard. The process involves repeatedly asking for a number until 0 is typed. The outputs are: a count of positives, a count of negatives, a count of zeros (the zeros that were part of the data, not the sentinel), and the average value. The program must also handle the empty-list scenario where only a 0 is entered.
在编写任何代码之前,我们必须完全理解程序应该做什么。输入是通过键盘输入的整数。处理过程包括反复询问数字,直到输入0为止。输出包括:正数的计数、负数的计数、零的计数(指数据中的零,而不是哨兵值),以及平均值。程序还必须处理空列表的情况,即只输入了一个0。
3. Decomposing the Task | 任务分解
Decomposition means breaking a large problem into smaller, manageable parts. For the Number List Analyser we can identify these sub-tasks: (1) initialise variables for counts and running sum, (2) create a loop that asks for a number and checks if it is the sentinel, (3) inside the loop, update the appropriate count based on whether the number is >0, <0 or ==0, (4) add the number to the running sum, (5) when the loop ends, calculate the average (if count >0), (6) display the results neatly.
分解意味着将一个大问题拆分成更小、更易管理的部分。对于数字列表分析器,我们可以识别出以下子任务:(1) 初始化计数变量和总和,(2) 创建一个循环,询问数字并检查是否为哨兵值,(3) 在循环内部,根据数字是否>0、<0或==0更新相应的计数,(4) 将数字加入总和,(5) 循环结束后,计算平均值(如果总数>0),(6) 整齐地显示结果。
4. Input and Output Specifications | 输入输出规格
Clear input and output definitions help avoid confusion later. Input: a sequence of integers, one per prompt, ending with 0. Output: text messages showing the total count, positive count, negative count, zero count, and average. If no numbers are entered before the sentinel, the program should output a message like “No numbers were entered.”
清晰的输入输出定义有助于避免以后的混淆。输入:一串整数,每次提示输入一个,以0结束。输出:显示总数、正数个数、负数个数、零个数和平均值的文本消息。如果在哨兵值之前没有输入任何数字,程序应输出类似“未输入数字”的消息。
5. Identifying Variables and Data Types | 识别变量和数据类型
We need to store counts and sums. Suitable variables are: total_count (integer), positive_count (integer), negative_count (integer), zero_count (integer), sum (integer or float, but sum of integers can be integer), number (integer for input), and average (float, because average might be a decimal). All counts start at 0. sum starts at 0. The number variable is updated each time the user gives input.
我们需要存储计数和总和。合适的变量有:total_count(总数,整数)、positive_count(正数计数,整数)、negative_count(负数计数,整数)、zero_count(零的计数,整数)、sum(总和,可以是整数,因为整数之和为整数)、number(输入的整数)和average(平均值,浮点数,因为平均值可能为小数)。所有计数初始值为0。sum初始值也为0。number变量在每次用户输入时更新。
6. Algorithm Design – Flowchart | 算法设计——流程图
A flowchart uses symbols to represent the steps of the algorithm. We can describe the logic using a structured text diagram. The main steps are: Start → Initialise all counts and sum to 0 → Input the first number → Decision: Is the number equal to 0? If yes, go to check total_count. If no, add 1 to total_count, then check: Is number > 0? If yes, increase positive_count; else if number < 0, increase negative_count; else increase zero_count. Then add number to sum. → Input next number and loop back to decision. → When number == 0, if total_count == 0 output “No numbers entered”, else compute average = sum / total_count and output all counts and average. → End.
流程图使用符号来表示算法的步骤。我们可以用结构化文本图示来描述逻辑。主要步骤为:开始→初始化所有计数和总和为0→输入第一个数字→判断:数字等于0吗?如果是,转去检查total_count。如果否,total_count加1,然后检查:数字>0吗?如果是,positive_count加1;否则如果数字<0,negative_count加1;否则zero_count加1。然后把数字加到sum上。→输入下一个数字并循环回到判断。→当数字==0时,如果total_count==0则输出“未输入数字”,否则计算 average = sum / total_count,并输出所有计数和平均值。→结束。
7. Algorithm Design – Pseudocode | 算法设计——伪代码
Pseudocode uses simple English-like statements to outline the logic. Here is a structured version:
伪代码使用简单的类英语语句来概述逻辑。以下是结构化版本:
SET total_count = 0
SET positive_count = 0
SET negative_count = 0
SET zero_count = 0
SET sum = 0
PRINT “Enter an integer (0 to stop):”
READ number
WHILE number != 0
total_count = total_count + 1
IF number > 0 THEN
positive_count = positive_count + 1
ELSE IF number < 0 THEN
negative_count = negative_count + 1
ELSE
zero_count = zero_count + 1
END IF
sum = sum + number
PRINT "Enter an integer (0 to stop):"
READ number
END WHILE
IF total_count == 0 THEN
PRINT "No numbers were entered."
ELSE
average = sum / total_count
PRINT "Total numbers: ", total_count
PRINT "Positives: ", positive_count
PRINT "Negatives: ", negative_count
PRINT "Zeros: ", zero_count
PRINT "Average: ", average
END IF
这段伪代码清楚地展示了变量初始化、带哨兵控制的循环、条件判断以及最终的平均值计算。
8. Walk-through with Sample Data | 示例数据走查
Let us test the algorithm manually with the inputs: 5, -2, 0, 3, -1, 0. First input 5 (not 0): total_count=1, positive_count=1, sum=5. Second input -2: total_count=2, negative_count=1, sum=3. Third input 0: zero_count=1, sum=3, total_count=3 (wait, 0 is not >0 or <0, so zero_count becomes 1). Fourth input 3: total_count=4, positive_count=2, sum=6. Fifth input -1: total_count=5, negative_count=2, sum=5. Sixth input 0 triggers loop exit. total_count=5, average=5/5=1. Output: positives=2, negatives=2, zeros=1, average=1.
让我们用输入:5, -2, 0, 3, -1, 0 手动测试算法。第一次输入5(不是0):total_count=1, positive_count=1, sum=5。第二次输入-2:total_count=2, negative_count=1, sum=3。第三次输入0:zero_count=1, sum=3, total_count=3。第四次输入3:total_count=4, positive_count=2, sum=6。第五次输入-1:total_count=5, negative_count=2, sum=5。第六次输入0导致循环退出。total_count=5, average=5/5=1。输出:正数=2,负数=2,零=1,平均值=1。
9. Testing and Edge Cases | 测试与边界情况
Testing should cover normal cases, boundary cases, and unexpected inputs. Normal case: mixed positive and negative numbers with some zeros. Boundary cases: only the sentinel 0 entered (total_count=0); only positive numbers; only negative numbers; only zeros before sentinel; one very large number; decimal numbers (if allowed, but specification says integers only). Our algorithm handles the empty input by checking total_count before calculating average, avoiding division by zero.
测试应涵盖正常情况、边界情况和意外输入。正常情况:正负混合以及一些零。边界情况:只输入哨兵值0(total_count=0);只输入正数;只输入负数;只输入零(在哨兵前);一个非常大的数字;小数(如果允许,但规格说仅整数)。我们的算法在计算平均值前通过检查total_count来处理空输入,避免了除以零的错误。
10. Implementing in Python (Optional) | Python实现(可选)
If you would like to see how the pseudocode translates into a real programming language, here is a Python 3 version. You can type it into a Python environment and run it.
如果你想看看伪代码如何转换成真正的编程语言,这里有一个Python 3版本。你可以将其输入Python环境并运行。
total_count = 0
positive_count = 0
negative_count = 0
zero_count = 0
sum_numbers = 0
print(“Enter an integer (0 to stop):”)
number = int(input())
while number != 0:
total_count += 1
if number > 0:
positive_count += 1
elif number < 0:
negative_count += 1
else:
zero_count += 1
sum_numbers += number
print("Enter an integer (0 to stop):")
number = int(input())
if total_count == 0:
print("No numbers were entered.")
else:
average = sum_numbers / total_count
print("Total numbers:", total_count)
print("Positives:", positive_count)
print("Negatives:", negative_count)
print("Zeros:", zero_count)
print("Average:", average)
请注意,input()函数返回字符串,因此我们使用int()将其转换为整数。如果用户输入非整数,程序会崩溃,但这就是我们要做的错误处理(一般不在七年级要求)。
11. Common Mistakes and Debugging Tips | 常见错误与调试技巧
When coding this yourself, you might forget to convert the input to an integer, causing a type error. Another common mistake is including the sentinel 0 in the counts or sum, which leads to incorrect results. Ensure you test the value before updating counts. Also, be careful to reset the number variable inside the loop, otherwise you may have an infinite loop. Use print statements to trace the values of variables during execution.
当你自己编写代码时,可能会忘记将输入转换为整数,导致类型错误。另一个常见错误是将哨兵值0计入计数或总和,导致结果不正确。确保在更新计数之前检查数值。此外,要注意在循环内部重新设置number变量,否则可能出现无限循环。可以使用打印语句在执行过程中追踪变量的值。
12. Summary and Reflection | 总结与反思
This case study covered all the key steps of computational thinking for a simple data analysis task. You saw how to break down a problem, design an algorithm using both a flowchart description and pseudocode, select variables, perform a manual dry run, and consider edge cases. The Number List Analyser program introduces loops, conditional statements, accumulators, and basic arithmetic – fundamental building blocks for any future programming projects. Try extending it: what if you wanted to find the largest and smallest numbers as well?
本案例分析涵盖了一个简单数据分析任务中计算思维的所有关键步骤。你看到了如何分解问题、使用流程图描述和伪代码设计算法、选择变量、进行手动演算并考虑边界情况。数字列表分析器程序引入了循环、条件语句、累加器和基本算术——这些是今后任何编程项目的基本构建块。试着扩展它:如果你想同时找到最大值和最小值,该怎么做呢?
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导