1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
您的最后一個(gè)選項(xiàng)將始終大于汽車列表的大小。
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
打印所有選項(xiàng)和退出選項(xiàng)。您需要在循環(huán)、循環(huán)和一個(gè)循環(huán)后提取索引。示例之一:
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");
添加回答
舉報(bào)