为什么我执行方法“ O”会导致程序崩溃?

问题描述 投票:-2回答:1
package trainbooking;
import java.util.Scanner;

public class Trainbooking {
    static final int SEATING_CAPACITY = 8;

    public static void displayMenu() {
        System.out.println("WELCOME TO OUR BOOKING SYSTEM");
        System.out.println("A) Add a customer to seat");
        System.out.println("V) View all the seats");
        System.out.println("E) Display Empty seats");
        System.out.println("D) Delete customer from seat");
        System.out.println("F) Find the seat for a given customers name");
        System.out.println("S) Store program data in to file");
        System.out.println("L) Load program data from file");
        System.out.println("O) View seats Ordered alphabetically by name");
        System.out.println("Q) Exit the application\n");
    }

    public static void V(String[]seats) {
        for (int i = 0; i < seats.length; i++) {
            System.out.println(seats[i]);
        }
    }

    public static void A(String[]seats) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of the customer you want to add? ");
        String customerName = input.next();
        System.out.println("What seat number do you want this customer to have?");
        int seatNumber = input.nextInt();
        seats[seatNumber] = customerName;
        System.out.println("Thank you, your record was submitted.");
    }

    public static void E() {
        // System.out.println("Empty seats");
        //for(int i=0;<seats.length;i++){
        //   System.out.println("current seat"+ " is ressrved by" + seats[i]);
        // }
    }

    public static void D() {}
    public static void F() {}
    public static void S() {}
    public static void L() {}

    public static void O() {
            int name;
            String temp;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter all of the pessenger names below; ");
            name = input.nextInt();
            String names[] = new String[name];
            Scanner b1 = new Scanner(System.in);
            System.out.println("Enter all the names:");
            for (int i = 0; i < name; i++) {
                names[i] = b1.nextLine();
            }
            for (int i = 0; i < name; i++) {
                for (int j = i + 1; j < name; j++) {
                    if (names[i].compareTo(names[j]) > 0) {
                        temp = names[i];
                        names[i] = names[j];
                        names[j] = temp;
                    }
                }
            }
            System.out.print("Following pessenger names have been Sorted in Order:");
            for (int i = 0; i < name - 1; i++) {
                System.out.print(names[i] + ",");
            }
            System.out.print(names[name - 1]);
        }
    }

    public static void main(String[]args) {
        String[]seats = new String[SEATING_CAPACITY];
        Scanner in = new Scanner(System.in);
        char choice;
        do {
            displayMenu();
            System.out.println("Enter a choice or press q to quit the application");
            choice = in.next().toUpperCase().charAt(0);
            switch (choice) {
            case 'A':
                A(seats);
                break;
            case 'V':
                V(seats);
                break;
            case 'E':
                E();
                break;
            case 'D':
                D();
                break;
            case 'F':
                F();
                break;
            case 'S':
                S();
                break;
            case 'L':
                L();
                break;
            case 'O':
                O();
                break;
            default:
                System.out.println("You have entered the wrong choice. Please try again!");
            }
        } while ((choice != 'Q'));
        System.out.println("Thank you for using our system.");
    }
}
java methods alphabetical
1个回答
0
投票

您可能希望此功能输入字符串/文本。

但是您将执行以下操作:您调用函数Scanner.nextInt()尝试从控制台读取Integer / Number值。

您应该使用input.nextLine()而不是input.nextInt()来执行操作,并且它不会再崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.