Java程序不会读取字符

时间:2019-03-20 03:16:13

标签: java java.util.scanner

这是我当前的代码,我省略了程序的前半部分。我正在尝试使用switch case语句来确定是否重复此代码段所在的while循环。此外,我的IDE(Eclipse)不会将“ Asker”识别为变量,并且不会让我运行或调试

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {

        int Customers = 1;
        boolean Repeat = true;
        Scanner keyboard = new Scanner(System.in);
        String CustomerInfo[][] = new String[1][6];
        // i = number j = name k = hotel m = location n = date p = discount type
        String CustomerData[][] = new String[1][4];
        // i = stay j = pre cost k = stay length m = final cost
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("Welcome to the Northwoods Hotel Reservation System");
        System.out.println("");

        while (Repeat) {

            for (int i = 0; i < Customers; i++) {

                System.out.println("Please Enter Your Name");
                CustomerInfo[i][0] = keyboard.nextLine();
                System.out.println("Please Enter Your Hotel");
                CustomerInfo[i][1] = keyboard.nextLine();
                System.out.println("Please Enter Your Reservation Date");
                CustomerInfo[i][2] = keyboard.nextLine();
                System.out.println("Please Enter The Location");
                CustomerInfo[i][3] = keyboard.nextLine();
                System.out.println("Please Enter Your Room Accomodations");
                CustomerInfo[i][4] = keyboard.nextLine();
                System.out.println("Please Enter Your Discount Type");
                CustomerInfo[i][5] = keyboard.nextLine();

            }    //End I for Loop

            System.out.println("Would you like to add another Customer?? (Y or N)");
            char asker = 'y';
            asker = keyboard.next().charAt(0);

            switch (asker) {

                case 'y':
                case 'Y':
                    Repeat = true;
                case 'n':
                case 'N':
                    Repeat = false;

                default:
                    Repeat = false;

            }    //End switch

        }    //End While
        //System.out.println(Asker);
        System.out.println(Repeat);
        System.out.println(Asker);
        System.out.println("");
        System.out.println("");
        System.out.println("Customer Number\t\t" + "Customer Name \t\t" + "Hotel\t\t" + "Reservation Date \t" + "Accomodations \t" + "Length of Reservation \t" + "Cost" + "Tax \t" + "Discount \t\t" + "Total Cost:");

        for (int i = 0; i < Customers; i++) {
            System.out.println(CustomerInfo[i][0] + " \t" + CustomerInfo[i][1] + " \t" + CustomerInfo[i][2] + " \t" + CustomerInfo[i][3] + " \t" + CustomerInfo[i][4] + " \t" + CustomerInfo[i][5]);

        }    //End For Loop

    }    //End Main Method
}    //End Class

1 个答案:

答案 0 :(得分:1)

Adeel提供了正确的解决方案:您需要在case语句中使用break;

        switch (asker) {

            case 'y':
            case 'Y':
                Repeat = true;
                break; // without a break here, it will fall through
            case 'n':
            case 'N':
                Repeat = false;
                break; // without a break here, it will fall through
                       // but this one isn't as bad since it is the same outcome
            default:
                Repeat = false;

        }    //End switch