循环播放以显示菜单 [英] Do while Loop to show a menu
本文介绍了循环播放以显示菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用do〜while(true)创建了菜单;但每次用户输入数字时,它不会运行程序,而是再次显示菜单!你觉得怎么样?
I have created my menu with do~while(true); but every time the user insert a number, instead of running the program it shows the the menu again! what do you think?
//我的主要方法
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方法
// getInput method
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;
}
}
推荐答案
因为您有 while(true);
,这意味着菜单将处于无限循环状态,直到调用中断为止
Because you have while(true);
, this means that the menu it will be in a infinite loop until a break is call.
尝试执行以下操作:
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
您可以使用 switch 处理输入,然后根据输入执行相应的操作.
You can use switch to process the input, and depending on input do the respective action.
这篇关于循环播放以显示菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文