Java多方法调用问题:第二个方法无法运行的求助咨询
问题分析与修复方案
你的代码核心问题是定义了两个public static void main(String[] args)方法,Java程序只能有一个入口方法,这会直接导致编译失败,使得oneRoundOfOrders方法根本无法执行。下面是修复后的完整代码及关键说明:
修复后的完整代码
// Author : Maya Strickland //Project Name : MCC Grub & Grill // Date: 09/27/2022 import java.util.Scanner; class Main { // 菜单商品价格常量 static final double smallPizza = 8.99; static final double mediumPizza = 11.99; static final double largePizza = 14.99; static final double veggieBurger = 5.99; static final double grilledChickenSandwich = 6.99; static final double hamburgerSandwich = 7.99; static final double macAndCheese = 2.49; static final double handCutFries = 2.75; static final double onionRings = 2.99; static final double foodTaxRate = 4.125; static double total = 0; static Scanner input; // 全局Scanner,避免重复创建 // 显示菜单的方法 static void showMenu() { System.out.println(""); System.out.println("Welcome to MCC Grub and Grill!"); System.out.println(""); System.out.println("Here is a list of our current menu items: "); System.out.println(""); System.out.println("Entrees: "); System.out.println("1. Small Pizza - Perfect size pizza for just one.....$8.99"); System.out.println("2. Medium Pizza - A pizza big enough to share, or not.....$11.99"); System.out.println("3. Large Pizza - A pizza big enough for the whole family!.....$14.99"); System.out.println("4. Veggie Burger - A black bean burger with Tomato, Mayo, Lettuce, and PepperJack Cheese. All grilled on a panini press.....$5.99"); System.out.println("5. Grilled Chicken Sandwich - Grilled Chicken topped with Tomato, Mayo, Lettuce, PepperJack Cheese. All grilled on a panini press.....$6.99"); System.out.println("6. Hamburger- Classic 100% beef patty with Tomato, Mayo, Lettuce and PepperJack Cheese. All grilled on a panini press.....$7.99"); System.out.println(""); System.out.println("Sides: "); System.out.println("7. Mac and Cheese - Not your momma's mac! Spicy 3 Cheese Mac made with Red Pepper Flakes. Beware the spice!.....$2.49"); System.out.println("8. Hand-Cut Fries - Crisp, golden fries deep fried in peanut oil.....$2.75"); System.out.println("9. Onion Rings - Crisp, golden rings of onion deep fried in peanut oil.....$2.99"); System.out.println(""); } // 处理单次下单的方法 static boolean processOrder() { 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(); switch (menuItem) { case 0: // 计算含税总价并显示 double tax = total * (foodTaxRate / 100); double finalTotal = total + tax; System.out.printf("当前未税总价: $%.2f\n", total); System.out.printf("税费: $%.2f\n", tax); System.out.printf("最终总价: $%.2f\n", finalTotal); return false; // 返回false表示结束下单 case 1: System.out.println("You've chosen small pizza! A perfect size pizza for just one! " + "$" + smallPizza); total += smallPizza; break; case 2: System.out.println("You've chosen medium pizza! A pizza big enough to share...or not " + "$" + mediumPizza); total += mediumPizza; break; case 3: System.out.println("You've chosen large pizza. A pizza big enough for the whole family! " + "$" + largePizza); total += largePizza; break; case 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; break; case 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; break; case 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; break; case 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; break; case 8: System.out.println("You've chosen Hand Cut Fries! Crisp, golden fries deep fried in peanut oil " + "$" + handCutFries); total += handCutFries; break; case 9: System.out.println("You've chosen Onion Rings! Crisp, golden rings of onion deep fried in peanut oil " + "$" + onionRings); total += onionRings; break; default: System.out.println("Please try again"); } return true; // 返回true表示继续下单 } // 唯一的程序入口main方法 public static void main(String[] args) { input = new Scanner(System.in); // 初始化Scanner showMenu(); // 先显示菜单 // 循环处理下单,直到用户输入0 while (processOrder()) { // 每次下单后显示当前总价 System.out.printf("当前未税总价: $%.2f\n\n", total); } input.close(); // 关闭Scanner } }
关键修改说明
- 移除重复的main方法:Java程序只能有一个入口方法,将两个main的逻辑合并,先调用菜单显示,再循环处理下单。
- 优化方法命名:把
myMethod改为showMenu,oneRoundOfOrders改为processOrder,让方法名更具语义性,提升代码可读性。 - 统一Scanner实例:将Scanner定义为类静态成员,避免每次下单都重复创建对象,节省资源。
- 增加循环下单逻辑:让
processOrder返回布尔值,在main中用while循环实现多次下单,直到用户输入0结束。 - 完善总价计算:用户输入0时自动计算税费和最终总价,完善点餐流程。
- 规范类名:Java类名首字母需大写,将
class main改为class Main,符合编码规范。
Java多方法使用核心要点
- 职责单一:每个方法只负责一件事,比如
showMenu仅显示菜单,processOrder仅处理单次下单,代码更清晰易维护。 - 调用顺序合理:在main入口方法中按业务逻辑依次调用其他方法,比如先展示菜单再处理下单。
- 静态方法配合:main是静态方法,调用的其他方法需为静态(或创建类实例调用非静态方法),静态变量可用于共享数据(如
total)。 - 避免重复代码:不要重复定义相同方法,也不要在多个方法中重复创建相同对象。
内容的提问来源于stack exchange,提问作者Maya Strickland
相关产品推荐
相关产品推荐

