程序提前终止异常咨询及MyProgrammingLab烘焙店编程题求解
Hey there! Let's break down your problem into two clear parts: finishing up that bakery sales program for MyProgrammingLab, and figuring out why your code is terminating earlier than expected.
1. Completing the Bakery Sales Program
Based on the typical structure of MyProgrammingLab problems, I’ll fill in the missing logic and provide a working example. The core requirement is to process customer purchases, decrementing stock for muffins or cupcakes as requested, until either all stock is sold out or there are no more purchase inputs.
Python Example
# Assume these initial stock values are pre-defined as per the problem muffins = 5 cupcakes = 3 # Loop to handle continuous customer inputs while True: try: # Read and clean up input purchase = input().strip() if not purchase: continue # Skip empty input lines if purchase == "muffin": if muffins > 0: muffins -= 1 print(f"sold muffin | remaining: {muffins} muffins, {cupcakes} cupcakes") else: print("sorry, muffins are sold out!") elif purchase == "cupcake": if cupcakes > 0: cupcakes -= 1 print(f"sold cupcake | remaining: {muffins} muffins, {cupcakes} cupcakes") else: print("sorry, cupcakes are sold out!") else: print("invalid input - please enter 'muffin' or 'cupcake'") # Exit early if all stock is gone (if required by the problem) if muffins == 0 and cupcakes == 0: print("all items sold out! closing up shop.") break except EOFError: # Handle end of input (e.g., Ctrl+D on Unix, Ctrl+Z on Windows) print("no more customers - program ending.") break
Java Example
If you’re working in Java instead, here’s an equivalent implementation:
import java.util.Scanner; public class BakerySales { public static void main(String[] args) { // Pre-defined initial stock int muffins = 5; int cupcakes = 3; Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String purchase = scanner.nextLine().trim(); if (purchase.isEmpty()) { continue; } if (purchase.equals("muffin")) { if (muffins > 0) { muffins--; System.out.printf("sold muffin | remaining: %d muffins, %d cupcakes%n", muffins, cupcakes); } else { System.out.println("sorry, muffins are sold out!"); } } else if (purchase.equals("cupcake")) { if (cupcakes > 0) { cupcakes--; System.out.printf("sold cupcake | remaining: %d muffins, %d cupcakes%n", muffins, cupcakes); } else { System.out.println("sorry, cupcakes are sold out!"); } } else { System.out.println("invalid input - please enter 'muffin' or 'cupcake'"); } // Exit early if all stock is exhausted if (muffins == 0 && cupcakes == 0) { System.out.println("all items sold out! closing up shop."); break; } } scanner.close(); System.out.println("no more customers - program ending."); } }
2. Troubleshooting Early Program Termination
Your code stopping before expected is almost always due to one of these common issues:
- No loop for continuous input: If you only read input once (e.g., a single
input()in Python orscanner.nextLine()in Java) instead of wrapping it in a loop, the program will exit after processing one customer. - Uncaught exceptions: If you try to decrement stock when it’s already 0, or don’t handle invalid input, your program might crash unexpectedly.
- Incorrect loop termination condition: For example, if you set your loop to stop when
muffins > 0instead of checking both stock values or waiting for end-of-input, it’ll exit as soon as muffins run out. - Accidental early exit: Maybe you added a
breakstatement in the wrong place, or your test input ended earlier than you realized (like pressing Ctrl+D/Z to stop input prematurely).
Double-check your code against these points—most of the time, fixing the loop structure or adding error handling will resolve the early termination issue.
内容的提问来源于stack exchange,提问作者Tree

