上一片里,💻小电脑有了时光机,改坏了能一键回滚。可它新写的成绩单小程序,一跑就出怪事。退回去躲不掉问题,得找出到底哪里坏了。这一片学捉虫:调试。Last leaf, 💻 Little Computer got a time machine — break something, roll it back. But its new class score app acts weird every time it runs. Rolling back won’t make the problem go away; we have to find what’s broken. This leaf is about bug hunting: debugging.
第 1 步:程序里的“虫子”Step 1: What’s a bug?
先认识对手——程序里的虫子长什么样?First, meet the enemy — what does a bug in a program look like?
程序里藏着的毛病,就叫 bug(虫子)。小电脑举起放大镜,一行一行地找。调试(debugging)就是捉虫:找到它、弄懂它、修好它。最常见的三种虫:名字拼错、漏了符号、条件写反。A mistake hiding in a program is called a bug. Little Computer raises its magnifying glass and checks line by line. Debugging means bug hunting: find it, understand it, fix it. The three most common bugs: a misspelled name, a missing symbol, a condition written backwards.
可程序一眨眼就跑完了,虫子到底躲在哪一行?But a program finishes in a blink — which line is the bug hiding in?
第 2 步:断点——按下暂停键Step 2: Breakpoints — hit pause
捉虫分三步:复现、定位、修复。复现,就是让虫子再露一次面。定位靠断点:在可疑那行左边点个红点。程序跑到这里就暂停,小电脑停下来看一眼变量。跟预想的不一样?虫子就在附近。没有调试器,在关键处 print 一下也行。Bug hunting takes three steps: reproduce, locate, fix. Reproduce means making the bug show up again. To locate it, set a breakpoint: click a red dot beside a suspicious line. The program pauses right there, and Little Computer peeks at its variables. Not what you expected? The bug is close. No debugger? A quick print at key spots works too.
等等,程序的毛病,为什么偏偏叫“虫子”?Wait — why is a program’s mistake called a “bug” at all?
第 3 步:第一只“真”虫子Step 3: The first real bug
1947 年,哈佛的 Mark II 计算机突然出了故障。工程师打开机柜,发现一只飞蛾卡在继电器里。他们把飞蛾贴进工作日志,写下:第一次真的找到虫子。这页日志至今还珍藏在美国的博物馆里。如今捉虫不用镊子,用的是编辑器里的调试器。In 1947, Harvard’s Mark II computer suddenly broke down. Engineers opened a cabinet and found a moth stuck in a relay. They taped it into the logbook and wrote: “First actual case of bug being found.” That page is still kept in a US museum today. Modern bug hunters skip the tweezers — they use the debugger built into their code editor.
🎮 你来帮小电脑捉虫(30 秒)🎮 Your turn: catch the bugs (30 seconds)
道理讲完了。成绩单小程序藏了 4 只虫子:每张卡片点出有虫的那一行,修好它。Theory done. The score app hides 4 bugs: on each card, click the buggy line to fix it.
一句话记住它:调试 = 复现 → 定位 → 修复,把程序里的虫子一只只捉出来。Remember it in one line: debugging = reproduce → locate → fix, catching a program’s bugs one by one.
bug 是程序里藏着的毛病:名字拼错、漏符号、条件写反A bug is a hidden mistake: misspelled names, missing symbols, backwards conditions断点让程序暂停,停下来看变量;print 也能帮忙定位Breakpoints pause the program so you can peek at variables; print helps too1947 年飞蛾卡进继电器,成了最有名的“第一只虫子”In 1947 a moth in a relay became the most famous “first bug”
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.