2

do〜while(true);を使用してメニューを作成しました。しかし、ユーザーが数字を挿入するたびに、プログラムを実行する代わりに、メニューが再び表示されます!どう思いますか?

//私のメインメソッド

public static void main(String[] args) {

    DataReader reader = new DataReader(); // The reader is used to read data from a file

    // Load data from the file
    if(reader.loadData(args[0])) {          // The filename is entered using a command-line argument

        vehicles= reader.getVehicleData(); // Store the arrays of Vehicle

        // Display how many shapes were read from the file
        System.out.println("Successfully loaded " + vehicles[0].getCount() + 
                           " vehicles from the selected data file!");
        displayMenu();
    }
}

//メニューメソッドを表示します

private static void displayMenu() {
    Scanner input = new Scanner(System.in);

    do {
        System.out.println("\n\n          Car Sales Menu");
        System.out.println("--------------------------------------");
        System.out.println("1 - Sort vehicles by owner's Last Name");
        System.out.println("2 - Sort vehicles by vehicle Model");
        System.out.println("3 - Sort vehicles by vehicle Cost\n");
        System.out.println("4 - List All Vehicles");
        System.out.println("5 - List All Cars");
        System.out.println("6 - List American Cars Only (Formal)");
        System.out.println("7 - List Foreign Cars only (Formal)");
        System.out.println("8 - List All Trucks");
        System.out.println("9 - List All Bicycles");
        System.out.print("\nSelect a Menu Option: ");
        getInput(input.next()); // Get user input from the keyboard 
    }
    while(true); // Display the menu until the user closes the program
}

//getInputメソッド

private static void getInput(String input) {
    switch(Convert.toInteger(input)) {
        case 1: // Sort Vehicles by Owner's Last Name
            Array.sortByOwnerName(vehicles);
            break;
        case 2: // Sort Vehicles by Vehicle Make & Model
            Array.sortByVehicleMakeModel(vehicles);
            break;
        case 3: // Sort Vehicles by Vehicle Cost
            Array.sortByVehicleCost(vehicles);
            break;
        case 4: // List All Vehicles
            displayVehicleData(0);
            break;

        default:
            System.out.print("The entered value is unrecognized!");
            break;
    }
}
4

4 に答える 4

3

があるのでwhile(true);、これは、ブレークが呼び出されるまでメニューが無限ループになることを意味します。

次のようなことを試してください。

 do {
        System.out.println("\n\n          Car Sales Menu");
        System.out.println("--------------------------------------");
        System.out.println("1 - Sort vehicles by owner's Last Name");
        System.out.println("2 - Sort vehicles by vehicle Model");
        System.out.println("3 - Sort vehicles by vehicle Cost\n");
        System.out.println("4 - List All Vehicles");
        System.out.println("5 - List All Cars");
        System.out.println("6 - List American Cars Only (Formal)");
        System.out.println("7 - List Foreign Cars only (Formal)");
        System.out.println("8 - List All Trucks");
        System.out.println("9 - List All Bicycles");
        System.out.print("\nSelect a Menu Option: ");
    try {
        int input = Integer.parseInt(getInput(input.next())); // Get user input from the keyboard 


        switch (input) {
        case 1:  // do something
                 break;
        case 2:  // do something
                 break;
        ...
       }
      } catch (NumberFormatException e) { ... }

    }
    while(true); // Display the menu until the user closes the program

スイッチを使用して入力を処理し、入力に応じてそれぞれのアクションを実行できます。

于 2012-11-23T22:01:52.037 に答える
2
while(true); // Display the menu until the user closes the program

while trueコメントに書いたことを正確に意味するわけではありません。その条件を確認するには、whileループに他の条件を追加する必要があります。その条件はinput、ユーザーから読み取ったものである必要があります。

たとえば、このようなもの。コードにも他の問題があるように思われるため、これで問題が完全に解決されない場合があることに注意してください。

int userInput = 0;
do {
    try {
        userInput = Integer.parseInt(getInput(input.next()));
    } catch (NumberFormatException e) {
        userInput = 0;
    }

} while (userInput < 1 || userInput > 9);

return userInput;  // For this you need to change return type of `displayMenu()`

次に、userInput返された main()メソッドを処理します。そこで、戻り値をローカル変数に格納する必要があります。

int userInput = displayMenu();
于 2012-11-23T22:01:48.963 に答える
1

whileループはwhile(true)、プログラムが強制的に中断されるまで常にループし続けるためです。関数の内容がなければgetInput()、ループは決して終了しないと言えます。

メソッド内またはメソッドの使用後にユーザーの入力を処理し、特定の基準が満たさgetInput()れたときに条件付きで抜け出す必要があります。while(true)

于 2012-11-23T22:02:57.217 に答える
1

メソッドが指示どおりに実行すると仮定するgetInputと、入力が読み込まれた後は、実際には何も実行していません。

したがって、ユーザーが値を入力すると、プログラムはその値を読み取り、それを無視して、メニューを再度実行します。

于 2012-11-23T22:03:09.397 に答える