随arraylist对象数量增减的选项菜单
Option menu that increases and decreases with number of arraylist objects
我已经创建了一个 for 循环,它列出了我的数组列表中的所有对象,但我需要它是一个始终以退出结束的动态选项菜单。我在程序的其他部分以不同的方法使用了 switch case 选项菜单。但是我不确定如何在这里创建递增的 case 开关,或者是否必须将它放入 for 循环中?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5s\n","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5s\n",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
我正在尝试创建一个菜单选项,这样我就可以 select 从我的数组列表中删除哪个对象
我希望输出看起来像这样:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
退出选项需要始终放在最后
您的最后一个选项将始终大于汽车列表的大小。
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
打印所有选项和一个退出选项。您需要在循环、循环和循环后的一个打印之外提取索引。范例之一:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5s\n",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
我已经创建了一个 for 循环,它列出了我的数组列表中的所有对象,但我需要它是一个始终以退出结束的动态选项菜单。我在程序的其他部分以不同的方法使用了 switch case 选项菜单。但是我不确定如何在这里创建递增的 case 开关,或者是否必须将它放入 for 循环中?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5s\n","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5s\n",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
我正在尝试创建一个菜单选项,这样我就可以 select 从我的数组列表中删除哪个对象 我希望输出看起来像这样:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
退出选项需要始终放在最后
您的最后一个选项将始终大于汽车列表的大小。
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
打印所有选项和一个退出选项。您需要在循环、循环和循环后的一个打印之外提取索引。范例之一:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5s\n",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");