You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现循环点餐累加总价的while循环问题求助

点餐程序累加功能修复方案

你的代码目前存在几个核心问题,导致无法实现多次点餐累加:

  • 每个菜品分支都用break直接跳出循环,点完一次就终止了
  • menuItem的输入放在循环外,循环内不会重新获取新的菜品选择
  • 用了两个Scanner实例,容易导致输入异常
  • 缺少用户选择继续点餐时的输入逻辑,循环条件里的order值没有更新
  • 总价变量total未初始化

以下是修复后的完整代码:

import java.util.Scanner;

public class OrderMenu {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // 初始化总价为0
        double total = 0.0;
        // 菜品价格定义
        double smallPizza = 8.99;
        double mediumPizza = 12.99;
        double largePizza = 16.99;
        double veggieBurger = 9.99;
        double grilledChickenSandwich = 10.99;
        double hamburgerSandwich = 9.99;
        double macAndCheese = 7.99;
        double handCutFries = 3.99;
        double onionRings = 4.99;

        System.out.println("Please enter 1 to order food from our menu or 0 to exit");
        int order = input.nextInt();

        if (order == 1) {
            while (order == 1) { 
                System.out.println("Please enter the menu number of the food you would like to order or 0 to stop ordering");
                int menuItem = input.nextInt();
                
                if (menuItem == 0) {
                    System.out.println("Total amount: $" + total);
                    break;
                } else if (menuItem == 1) {
                    System.out.println("You've chosen small pizza! A perfect size pizza for just one! $" + smallPizza);
                    total += smallPizza;
                } else if (menuItem == 2) {
                    System.out.println("You've chosen medium pizza! A pizza big enough to share...or not $" + mediumPizza);
                    total += mediumPizza;
                } else if (menuItem == 3) {
                    System.out.println("You've chosen large pizza. A pizza big enough for the whole family! $" + largePizza);
                    total += largePizza;
                } else if (menuItem == 4) {
                    System.out.println("You've chosen Veggie Burger! A black bean burger with Tomato, Mayo, Lettuce, and PepperJack Cheese. All grilled on a panini press. $" + veggieBurger);
                    total += veggieBurger;
                } else if (menuItem == 5) {
                    System.out.println("You've chosen Grilled Chicken Sandwich! Grilled Chicken topped with Tomato, Mayo, Lettuce, PepperJack Cheese. All grilled on a panini press $" + grilledChickenSandwich);
                    total += grilledChickenSandwich;
                } else if (menuItem == 6) {
                    System.out.println("You've chosen a Hamburger! Classic 100% beef patty with Tomato, Mayo, Lettuce and PepperJack Cheese. All grilled on a panini press $" + hamburgerSandwich);
                    total += hamburgerSandwich;
                } else if (menuItem == 7) {
                    System.out.println("You've chosen Mac and Cheese! Not your momma's mac! Spicy 3 Cheese Mac made with Red Pepper Flakes. Beware the spice! $" + macAndCheese);
                    total += macAndCheese;
                } else if (menuItem == 8) {
                    System.out.println("You've chosen Hand Cut Fries! Crisp, golden fries deep fried in peanut oil $" + handCutFries);
                    total += handCutFries;
                } else if (menuItem == 9) {
                    System.out.println("You've chosen Onion Rings! Crisp, golden rings of onion deep fried in peanut oil $" + onionRings);
                    total += onionRings;
                } else {
                    System.out.println("Invalid Choice! Try Again!");
                    // 输入无效时,跳过后续步骤,直接回到循环开头重新获取菜品选择
                    continue;
                }

                // 询问是否继续点餐
                System.out.println("Please enter a 1 to order more food or 0 to proceed to checkout");
                order = input.nextInt();
            }
        }
        // 关闭Scanner
        input.close();
    }
}

关键修改说明:

  • 移除多余的Scanner实例,只用一个input处理所有输入,避免输入流异常
  • 将menuItem的输入移到while循环内部,每次循环都会重新获取用户的菜品选择
  • 去掉每个菜品分支里的break,只在用户输入0(停止点餐)时才跳出循环
  • 在每次点完有效菜品后,询问用户是否继续点餐,并更新order变量的值,控制循环是否继续
  • 初始化total变量为0,确保累加从正确的初始值开始
  • 输入无效菜品时,用continue跳过后续步骤,直接回到循环开头重新获取输入

内容的提问来源于stack exchange,提问作者Maya Strickland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:05:45