我在餐馆系统和管理系统工作,但它目前是餐厅系统。基本上,我正在尝试创建一个空列表,以便我可以添加项目,而不必说它必须在列表中有一定数量的项目。 我环顾四周,但作为初学者,我发现在某些问题上难以理解答案。 如果有人能告诉我如何将它合并到我的代码中,那就太棒了!在此先感谢:)
import java.util.Scanner;
class RestaurantMain {
public static void main(String[] args)
{
//Variables//
int choice;
int customerChoice;
boolean trueFalse;
int restart = 0;
String choice2;
//EndVariables//
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Cooper's restaurant system!");
System.out.println("How can I help?");
System.out.println("");
System.out.println("1. Customer System");
System.out.println("2. Management System");
System.out.println("");
System.out.println("");
System.out.print("Which option do you choose: ");
choice = in.nextInt();
if (choice == 1) {
System.out.println("Our menu's are as follows:");
System.out.println("");
System.out.println("1. Drinks");
System.out.println("2. Starters");
System.out.println("3. Mains");
System.out.println("4. Desserts");
System.out.println("");
System.out.print("What menu would you like to follow? ");
customerChoice = in.nextInt();
if (customerChoice == 1) {
System.out.println("Drinks Menu");
System.out.println("Would you like to order? ");
in.nextLine();
}
if (customerChoice == 2) {
System.out.println("Starters Menu");
}
if (customerChoice == 3) {
System.out.println("Mains menu");
}
if (customerChoice == 4) {
System.out.println("Desserts Menu");
}
}
答案 0 :(得分:1)
尝试使用ArrayList。它就像一个数组,但具有动态大小。
另见:http://www.tutorialspoint.com/java/java_arraylist_class.htm
答案 1 :(得分:0)
尝试ArrayList
课程。您可以在施工时设置初始尺寸,但随着它变满,它会随时调整大小。它的行为与数组相同,但是当空间不足时它会“增长”。
答案 2 :(得分:0)
我不知道,为什么你想要List。我确信更好地使用数组和静态方法。处理一个菜单项的一种方法。为什么更好?因为如果你将使用许多if-else,你会得到一个意大利面条代码(非结构化)。 Java是非常强大的语言,只需创建其他类来处理您的菜单项,使用静态方法就会很开心!祝你好运!
import java.util.Scanner;
class RestaurantMain {
//created static variables for using arrays inside static methods of the class
static String[] menuMainItems = {
"Customer System",
"Management System"
};
static String[] menuItems = {
"Drinks Menu",
"Starters Menu",
"Mains menu",
"Desserts Menu"
};
static String[] menuCustomer = {
"Drinks",
"Starters",
"Mains",
"Desserts"
};
public static void main(String[] args)
{
//Variables//
int choice = 0;
int customerChoice = 0;
boolean trueFalse = false;
int restart = 0;
String choice2 = "";
//EndVariables//
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Cooper's restaurant system!");
System.out.println("How can I help?\n"); // \n - is just symbol of end line in ascii
//print our list from array
for(int i = 0; i < menuMainItems.length; i++)
System.out.println( (i+1) + ". " + menuMainItems[i] );
System.out.print("Which option do you choose: ");
choice = in.nextInt();
switch(choice)
{
case 1:
handleCustomerSystem(in); //it's better to use methods
break; //don't forget this! It's important!
case 2:
//and so on
break;
}
}
//the handling of the first item
private static void handleCustomerSystem(Scanner in)
{
int customerChoice = 0;
System.out.println("Our menu's are as follows:\n"); // \n - is just symbol of end line in ascii
//print our list from array
for(int i = 0; i < menuItems.length; i++)
System.out.println( (i+1) + ". " + menuItems[i] ); // (i+1) because we wanna to see the count from 1, not 0
System.out.print("\nWhat menu would you like to follow? ");
customerChoice = in.nextInt();
//and again using switch
switch(customerChoice)
{
case 1:
System.out.println(menuItems[customerChoice]);
in.nextLine();
break;
case 2:
//ans so on
break;
}
}
}