在java程序中创建一个循环

时间:2015-06-16 10:09:39

标签: java

我正在尝试编写一个程序,在该程序中,用户将首先显示一个菜单,从中可以选择多个选项。我试图创建一个代码,一旦选择了一个选项,将返回用户菜单。但是,程序只是结束而不是将用户循环回菜单。我需要将什么添加到代码中,以便一旦选择方法运行它将使用户返回选择菜单?

以下摘要显示了该计划的相关部分:

    Scanner scanner = new Scanner(System.in);

    int selection = 0;


    while (x == 0) {
        System.out.println("Please make a selection");
        System.out.println("[1] Make a new booking");
        System.out.println("[2] View films");
        System.out.println("[3] View Screens");
        System.out.println("[4] View Customers");
        System.out.println("[5] View showing information");
        System.out.println("[6] View booking information");
        System.out.println("[7] Exit");

        while (selection == 0) {
            System.out.println("Selection: ");
            try {
                selection = scanner.nextInt();

            } catch (InputMismatchException nfe) {
                System.out.println("Input must be a number");
                selection = 0;
                scanner = null;
                scanner = new Scanner(System.in);
            }
        }
        switch (selection) {

            case 1:

                System.out.println("All available showings:");
                x = 1;

                for (Showing aShowing : showingList) {
                    System.out.println(aShowing.toString());
                }

                System.out.println("Enter the showing number: ");
                int show = scanner.nextInt();

                Showing userChoice = showingList.get(show - 1);

                System.out.println("Enter the number of tickets: ");

                int tickets = scanner.nextInt();


                Booking booking1 = new Booking(customerA, userChoice, tickets);

                System.out.println(booking1);


                break;

            case 2:
                System.out.println("Current films being shown:");
                x = 1;
                System.out.println("Film one: " + filmA.getTitle()
                        + ".\nLength: " + filmA.getDuration() + " minutes."
                        + "\nAge Rating: " + filmA.getAgeRating()
                        + ".\nDescription: " + filmA.getDescription() + ".");
                break;

            case 3:
                System.out.println("Screen information:");
                x = 1;
                System.out.println("Screen id:" + screenA.getId()
                        + ".\nSeats a maximum of " + screenA.getCapacity() + " people.");
                break;

1 个答案:

答案 0 :(得分:2)

我不明白while(x==0)在那里做了什么。

我从您的要求中了解到,您希望为用户提供一个菜单,其中包含选项和用户需要输入他的选择,然后将根据该操作执行操作,然后再次请求用户提供其他输入。

如果是这种情况,那么尝试按照以下方式编写逻辑,

do{
    System.out.println("Please make a selection");
    System.out.println("[1] Make a new booking");
    System.out.println("[2] View films");
    System.out.println("[3] View Screens");
    System.out.println("[4] View Customers");
    System.out.println("[5] View showing information");
    System.out.println("[6] View booking information");
    System.out.println("[7] Exit");

    selection = scanner.nextInt();

    switch(selection){

        case 1:
           ...
        //so on

        case 7:
           //do nothing
           break;
        default:
           System.out.println("Wrong number entered");

    }



}while(selection!=7);