新 List/Array 没有一定数量的号码?

New List/Array without a set amount of numbers?

我在做餐厅系统和管理系统,不过目前是餐厅系统。基本上,我试图创建一个空列表,这样我就可以向其中添加项目,而不用说它必须在列表中包含一定数量的项目。 我环顾四周,但作为初学者,我发现很难理解某些问题的答案。 如果有人可以告诉我如何将它合并到我的代码中,那就太好了!提前致谢:)

    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");
                    }



            }

尝试使用 ArrayList。它就像一个数组,但具有动态大小。

另见:http://www.tutorialspoint.com/java/java_arraylist_class.htm

试试 ArrayList class。您可以在构建时设置初始大小,但它会在变满时随时调整大小。它的行为与数组相同,但是当用完 space.

时它会 'grows'

我不知道,你为什么要列出。我确信最好使用数组和静态方法。处理一个菜单项的一种方法。为什么更好?因为如果你将使用很多很多 if-else,你会得到一个意大利面条代码(非结构化)。 Java 是非常强大的语言,只需创建其他 class 来处理您的菜单项,使用静态方法,您会很高兴!祝你好运!

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;
        }
    }
}