提问人:G. Parker 提问时间:8/31/2023 更新时间:9/1/2023 访问量:51
Java 食物菜单 - 无法存储用户输入和查看后
Java food menu - unable to store user input and review after
问:
我是 Java 的新手,已经开始了一个创建食物菜单的基本项目。我已经设法让用户选择披萨配料的第一部分和这种存储,以便用户可以在主菜单上查看订单,但是我在尝试存储饮料选项时遇到了问题。我为披萨、饮料和 viewOrder 设置了单独的课程,如果需要,我可以将其复制到聊天中。感谢任何关于该做什么以及如何设置代码的反馈,因为我渴望学习!
用户选择饮料后,它会将他们带回主菜单。选择视图顺序时,它会返回错误消息,指出其顺序为空。我认为这可能与视图顺序中的if语句有关。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int userInput; // Declare the variable outside the loop
String selectedPizza = null;
ArrayList<String> selectedToppings = new ArrayList<>();
ArrayList<String> selectedDrinks = new ArrayList<>(); // Declare the ArrayList for drinks
while (true) {
Order.orderMenu(); // Display the menu
userInput = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
if (userInput == 1) {
FoodMenu.displayMenu();
int userInput1 = scanner.nextInt();
scanner.nextLine();
if (userInput1 == 1) {
selectedPizza = "Pizza"; // Store the selected food option
// Collect user's topping selections
int toppingsSelected = 0;
while (toppingsSelected < 4) { // asks the user to select 4 toppings
System.out.print("Select a topping: ");
String topping = scanner.nextLine(); // user input
selectedToppings.add(topping); // adds toppings selected
toppingsSelected++;
}
} else if (userInput1 == 2) {
// User gets given drinks options to order here
Beverages.drinkOptions();
String selectedDrink = Beverages.getDrinkChoice(scanner);
if (selectedDrink != null) {
selectedDrinks.add(selectedDrink);
} else {
System.out.println("Please enter a valid drink number.");
}
} else {
System.out.println("Please enter a valid selection.");
}
} else if (userInput == 2) {
// View order logic
if (selectedPizza != null) {
OrderView.viewOrder(selectedPizza, selectedToppings, selectedDrinks);
} else {
System.out.println("Your order is empty, please add an item from the menu first.");
}
} else if (userInput == 3) {
System.out.println("Thank you, your order is on the way");
break;
} else {
System.out.println("Please enter a valid selection.");
}
}
scanner.close();
}
}
答:
0赞
Sarah
8/31/2023
#1
问题似乎出在显示空订单之前如何检查空订单。您正确地检查了 selectedPizza 是否为 null,但您还需要检查 selectedToppings 或 selectedDrinks 是否为空。下面是用于解决此问题的修改后的视图顺序逻辑:
} else if (userInput == 2) {
// View order logic
if (selectedPizza != null || !selectedToppings.isEmpty() || !selectedDrinks.isEmpty()) {
OrderView.viewOrder(selectedPizza, selectedToppings, selectedDrinks);
} else {
System.out.println("Your order is empty, please add an item from the menu first.");
}
}
而且,下一期可能是在您的饮料课上。您提到您有一个 getDrinkChoice 方法。确保此方法正确处理用户输入并返回所选饮料。下面是该方法的外观示例:
public class Beverages {
public static String getDrinkChoice(Scanner scanner) {
int userInput = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (userInput) {
case 1:
return "Coke";
case 2:
return "Sprite";
case 3:
return "Water";
default:
return null; // Return null for invalid choices
}
}
}
祝你好运。
评论