Java 执行while循环以显示菜单

Java 执行while循环以显示菜单,java,methods,do-while,Java,Methods,Do While,我已经用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.lo

我已经用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;
    }
}
而true
并不完全代表您在评论中所写的内容。您需要在while循环中添加一些其他条件来检查该条件。该条件应在从用户读取的
输入上

例如,类似这样的东西。请注意,这可能无法完全解决您的问题,因为您的代码似乎还存在一些其他问题:-

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()`

因为您有
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

您可以使用来处理输入,并根据输入执行相应的操作。

因为您的while循环是
while(true)
它将始终保持循环,直到程序被强制中断。如果没有
getInput()
函数的内容,可以说循环永远不会结束


您需要在
getInput()
方法中或在使用它之后处理用户的输入,然后在满足某些条件时有条件地中断
while(true)

假设您的
getInput
方法执行它所说的操作,一旦输入被读入,您实际上就没有对其进行任何处理


因此,当用户输入一个值时,您的程序会读取该值,愉快地忽略它,然后再次运行菜单。

更合理。。。让我查一下
int userInput = displayMenu();
 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