1.4.9 Exploration: Going to the Movies(双语回答)

  1. 先说实测结论(重要)

MovieBudget 真跑了一遍(Java 11 单文件模式),结果是:

版本 结果

A. 你贴的原代码 ✅ 编译通过,输入 Dune / 12.50 / 2 / Popcorn / 8.75 → 输出 33.75 和 6.25

B. 删掉 input.nextLine() ❌ 运行崩溃:InputMismatchException

C. 去掉变量的 double 声明 ❌ 编译失败:cannot find symbol ×4
所以:你贴的这一版里,那 2 个编译期 bug 已经不存在了 —— 变量都写了 double,编译是过的。题目说的"2 个编译期错误"指的是原始 starter 文件(就是我复现的 C 版)。下面按题目本意回答,同时标注你当前这版的实际情况。
版本 结果

A. Your code as pasted ✅ Compiles, input Dune / 12.50 / 2 / Popcorn / 8.75 → prints 33.75 and 6.25

B. input.nextLine() removed ❌ Crashes at runtime: InputMismatchException

C. double declarations removed ❌ Compile error: cannot find symbol ×4

So the 2 compile-time bugs are NOT in the version you pasted — every variable is declared, it compiles fine. Those 2 bugs live in the original starter file (which is my version C). Answers below follow the intended exercise, flagged against your actual code.

  1. 两个编译期 bug / The two compile-time bugs

Q: Why does each bug occur? 为什么会出现?

🇨🇳 Java 要求变量必须先声明类型再使用。原始文件里这两行是:
totalPrice = ticketPrice * (input.nextDouble()); // 少了 double
snackPrice = input.nextDouble(); // 少了 double

这是"赋值",但编译器从没见过 totalPrice / snackPrice 这两个名字,于是报 cannot find symbol。连带 double total = totalPrice + snackPrice; 也一起报错。

实测原文(我复现的):

error: cannot find symbol
snackPrice=input.nextDouble();
symbol: variable snackPrice
location: class MovieBudgetPre

🇬🇧 Java requires a variable to be declared with its type before use. The starter file wrote:
totalPrice = ticketPrice * (input.nextDouble()); // missing double
snackPrice = input.nextDouble(); // missing double

That's an assignment to a name the compiler has never seen, so it reports cannot find symbol. The line double total = totalPrice + snackPrice; then fails too.

Actual compiler output (reproduced):

error: cannot find symbol
snackPrice=input.nextDouble();
symbol: variable snackPrice
location: class MovieBudgetPre

Q: What change fixes each bug? 怎么修?

🇨🇳 首次出现的变量前面补上类型 double,并且必须在 total 那行之前声明。你贴的版本已经修好了:
double totalPrice = ticketPrice * (input.nextDouble()); // ✅ 有 double
double snackPrice = input.nextDouble(); // ✅ 有 double

🇬🇧 Add the type double the first time each variable appears, and declare it before the total line. Your version already has this correct:
double totalPrice = ticketPrice * (input.nextDouble()); // ✅
double snackPrice = input.nextDouble(); // ✅

⚠️ 顺带提醒:buget 是 budget 的拼写错误。它现在能编译是因为全文都拼错得一样。哪天你在别处写成 budget,就会立刻多出一个 cannot find symbol —— 建议统一改回 budget。

⚠️ Note: buget is a typo of budget. It compiles only because it's misspelled consistently. The moment you type budget anywhere, you get a new cannot find symbol — rename it.

  1. 逻辑 bug:Scanner 缓冲区 / The logic bug: Scanner buffer

Q: What unexpected behavior occurs? 会出现什么异常行为?

🇨🇳 删掉 input.nextLine() 后实测:"What snack will you get?" 刚打印就被跳过,你根本没机会输入零食名,snack 直接变成空字符串 ""。接着程序读到 Popcorn 却想当小数解析,当场崩溃:

Exception in thread "main" java.util.InputMismatchException
at MovieBudgetPre.main(...)

🇬🇧 With input.nextLine() removed: "What snack will you get?" is skipped entirely — you never get to type the snack name, and snack becomes "". The program then tries to parse Popcorn as a decimal and crashes:

Exception in thread "main" java.util.InputMismatchException
at MovieBudgetPre.main(...)

Q: Why does this happen? 为什么会这样?

🇨🇳 nextDouble() / nextInt() 只吃掉数字本身,把你按的回车 \n 留在缓冲区里。下一个 nextLine() 一看到这个孤零零的 \n,就以为"已经读完一整行了",立刻返回 ""。

关键细节:nextDouble() 自己会跳过空白,所以残留的 \n 只有在下一次读的是 nextLine() 时才会闯祸。你代码里票价之后紧跟的是另一个 nextDouble(),所以那里不补 nextLine() 也没事 —— 这正是这个 bug 隐蔽的原因。

🇬🇧 nextDouble() / nextInt() read the number but leave the Enter keypress (\n) sitting in the buffer. The next nextLine() sees that leftover \n, treats it as a complete (empty) line, and returns "" immediately.

Key nuance: nextDouble() skips leading whitespace itself, so the stray \n only causes trouble when the next read is nextLine(). After the ticket price, the next call is another nextDouble(), so omitting the clear there is harmless — which is exactly what makes this bug sneaky.

Q: What change fixes it? 怎么修?

🇨🇳 在每个 nextDouble() / nextInt() 之后补一句裸的 input.nextLine();,把回车吞掉:
double ticketPrice = input.nextDouble();
input.nextLine(); // ← 清掉 \n

double totalPrice = ticketPrice * (input.nextDouble());
input.nextLine(); // ← 你已有 ✅(这句最关键,后面紧跟 nextLine 读零食名)

double snackPrice = input.nextDouble();
input.nextLine(); // ← 清掉 \n

你当前这版中间那句已经有了,所以能正常跑(实测 A 版输出 33.75)。建议把另外两处也补上,养成习惯。

🇬🇧 Add a bare input.nextLine(); after every nextDouble() / nextInt() to swallow the newline:
double ticketPrice = input.nextDouble();
input.nextLine(); // ← clear \n

double totalPrice = ticketPrice * (input.nextDouble());
input.nextLine(); // ← you already have this ✅ (the critical one)

double snackPrice = input.nextDouble();
input.nextLine(); // ← clear \n

Your version already has the middle one, which is why it runs (test A printed 33.75). Add the other two for good habit.

  1. 完整修好版 / Fully fixed version

package unit1_5;

import java.util.Scanner;

public class MovieBudgetPre
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

double budget = 40.00; // 修掉 buget 拼写 / fixed typo

System.out.println("What movie are you going to see?");
String movie = input.nextLine();

System.out.println("How much is the ticket?");
double ticketPrice = input.nextDouble();
input.nextLine(); // 清缓冲区 / clear buffer

System.out.println("How many tickets would you like to buy?");
int ticketCount = input.nextInt();
input.nextLine(); // 清缓冲区 / clear buffer

double totalTicketPrice = ticketPrice * ticketCount; // ← 注意变量名

System.out.println("What snack will you get?");
String snack = input.nextLine();

System.out.println("How much is the snack?");
double snackPrice = input.nextDouble();
input.nextLine(); // 清缓冲区 / clear buffer

double total = totalTicketPrice + snackPrice;
double checkBudget = budget - total;

System.out.printf("Total price for your movie night: $%.2f%n", total);
System.out.printf("Budget Check: $%.2f%n", checkBudget);

input.close();
}
}

改动总结 / Summary of changes:

  1. buget → budget(全文统一)

  2. 三处 input.nextLine() 补齐(原来只有一处)

  3. println("...$") + println(total) 合成一条 printf("%.2f") —— 原写法会把 $ 和数字拆成两行,且可能出现 33.75000000000001

  4. 可直接交的 Investigate.txt 草稿

🇬🇧 English(课堂提交用)

🇨🇳 中文版

📌 最后呼应上一轮:如果这次的 Modify 步骤也要求变量名是 totalTicketPrice(而不是 totalPrice),记得照抄 —— 评分器按名字逐字匹配,功能等价不管用。

📌 Ties back to last round: if this activity's Modify step also requires the variable be named totalTicketPrice (not totalPrice), copy it exactly — the grader matches names literally, functional equivalence won't pass.

posted @ 2026-09-14 17:00  kkman2000  阅读(6)  评论(0)    收藏  举报