Java披萨程序开发问题:移除已选配料后不再显示菜单选项
披萨定制程序:隐藏已选配料的解决方案
核心问题解决思路
要实现「选完的配料不再显示在菜单里」,可以新增一个布尔数组标记每个配料的选中状态。每次打印菜单时仅输出未被标记为已选的配料;用户选择配料后,将对应位置的标记设为true,后续循环就不会再显示该配料。
修改后的完整代码
import java.util.Scanner; public class PizzaCustomizer { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("\"---------------------\""); System.out.println("\"Create Your Own Pizza\""); System.out.println("\"---------------------\""); char userChar = 'y'; String[] toppings = {"\"diced onion\"", "\"green pepper\"", "\"pepperoni\"", "\"sliced mushrooms\"", "\"jalapenos\"", "\"pineapple\"", "\"dry red pepper\"", "\"basil\""}; String[] choices = { "\"A. \"", "\"B. \"", "\"C. \"", "\"D. \"", "\"E. \"", "\"F. \"", "\"G. \"", "\"H. \""}; // 新增数组标记配料是否已被选择 boolean[] isToppingSelected = new boolean[toppings.length]; while (userChar == 'y') { System.out.println("\"\nChoose your favorite toppings\""); // 仅打印未被选中的配料 for (int i=0; i<choices.length; ++i) { if (!isToppingSelected[i]) { System.out.println(choices[i] + toppings[i]); } } System.out.println("\"\nEnter a choice: \""); String userToppings = sc.nextLine(); // 去掉输入中的引号和空白,避免判断出错 userToppings = userToppings.replace("\"", "").trim(); if (userToppings.equalsIgnoreCase("A")) { System.out.println("\"Please choose one amount: \""); System.out.println("\"a. 1/8 cup b. 1/4 cup\""); String amountChoice = sc.nextLine().replace("\"", "").trim(); choices[0] = amountChoice.equalsIgnoreCase("A") ? "\"1/8 cup\"" : "\"1/4 cup\""; // 标记该配料已选 isToppingSelected[0] = true; System.out.println("\"Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.\""); // 处理next()后的换行残留问题 userChar = sc.next().charAt(0); sc.nextLine(); } else if (userToppings.equalsIgnoreCase("B")) { System.out.println("\"Please choose one amount: \""); System.out.println("\"a. 1/8 cup b. 1/4 cup\""); String amountChoice = sc.nextLine().replace("\"", "").trim(); choices[1] = amountChoice.equalsIgnoreCase("A") ? "\"1/8 cup\"" : "\"1/4 cup\""; isToppingSelected[1] = true; System.out.println("\"Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.\""); userChar = sc.next().charAt(0); sc.nextLine(); } else if (userToppings.equalsIgnoreCase("C")) { // 按A、B的格式继续补充C到H的逻辑 System.out.println("\"Please choose one amount: \""); System.out.println("\"a. 1/8 cup b. 1/4 cup\""); String amountChoice = sc.nextLine().replace("\"", "").trim(); choices[2] = amountChoice.equalsIgnoreCase("A") ? "\"1/8 cup\"" : "\"1/4 cup\""; isToppingSelected[2] = true; System.out.println("\"Would you like to add more toppings? Type Y for yes or any other key to submit your recipe.\""); userChar = sc.next().charAt(0); sc.nextLine(); } // 此处继续补充D到H的判断逻辑... else { System.out.println("\"You did not enter a valid topping choice\""); } } // 补全未定义的食谱变量(示例值) String crustType = "\"Thin Crust\""; String sauceType = "\"Tomato Sauce\""; String sauceAmount = "1/2 cup"; String cheese = "\"Mozzarella Cheese\""; String cheeseAmount = "1 cup"; System.out.println("\"Your pizza recipe is: \""); System.out.println(crustType + "\"\t1 \""); System.out.println(sauceType + "\"\t\"" + sauceAmount); System.out.println(cheese + "\"\t\t\"" + cheeseAmount); // 仅打印用户实际选择的配料 for (int i=0; i<toppings.length; i++) { if (isToppingSelected[i]) { System.out.println(toppings[i] + "\"\t\t\"" + choices[i]); } } } }
关键修改点说明
- 选中状态标记:新增
boolean[] isToppingSelected数组,初始值全为false,用户选完配料后将对应位置设为true。 - 菜单过滤打印:循环打印菜单时增加
if (!isToppingSelected[i])判断,只显示未被选择的配料。 - 输入稳定性优化:去掉输入字符串中的引号和空白,避免用户输入带/不带引号导致判断失败;处理
sc.next()后的换行残留,防止后续输入读取异常。 - 食谱输出优化:打印最终食谱时仅输出用户选择的配料,避免无效信息干扰。
额外可读性与功能性建议
- 规范命名:将类名
test改为PizzaCustomizer,变量名使用语义化命名(如userToppings改为selectedOption),符合Java编码规范。 - 补全配料逻辑:当前代码仅处理了A、B、C选项,需按相同格式补全D到H的判断逻辑。
- 简化重复代码:虽然还没学自定义方法,但可以把配料选择、用量设置的重复代码暂时复制复用,后续学习方法后再统一重构。
内容的提问来源于stack exchange,提问作者L3i5ur3L3
相关产品推荐
相关产品推荐

