sách gpt4 ai đã đi

java - 执行 while 循环以显示菜单

In lại 作者:行者123 更新时间:2023-12-04 00:53:37 28 4
mua khóa gpt4 Nike

我已经用 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);

LÀM {
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
}

//获取输入法

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

mặc định:
System.out.print("The entered value is unrecognized!");
phá vỡ;
}
}

1 Câu trả lời

因为你有 while(true);,这意味着菜单将处于无限循环中,直到调用 break。

尝试做类似的事情:

 LÀM {
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: ");
thử {
int input = Integer.parseInt(getInput(input.next())); // Get user input from the keyboard


switch (input) {
case 1: // do something
phá vỡ;
case 2: // do something
phá vỡ;
...
}
} catch (NumberFormatException e) { ... }

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

Bạn có thể sử dụng công tắc处理输入,并根据输入执行相应的操作。

关于java - 执行 while 循环以显示菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13536206/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com