public static void getVegetableFromUser()
{
String[] veg = SandwichDB.getVegetable();
HashMap<String, String> getMapVegPrice = SandwichDB.getMapVegPrice();
int vegSave = Validator.getInt(Scanner, "Select Vegetables [1, 8]: ", 1, 8);
String v = veg[vegSave-1];
sandwich.setVegetables(v);
}
好的,所以控制台打印出来
=== 选择三明治蔬菜 ===
1 个红洋葱 $0.10
2 个橄榄 $0.10
3 个泡菜 $0.10
4 个生菜 $0.20
5 个青椒 $0.25
6 个西红柿 $0.30
7 个奶酪 $0.49
8 退出蔬菜部分
选择蔬菜 [1, 8]:
这个想法是能够选择多种蔬菜,直到我选择了 8 种蔬菜,即退出蔬菜部分。我该怎么做呢?使用 else-if 语句?
此外,蔬菜应该是一个数组,而不是一个字符串......就像这一行:String v = veg[vegSave-1]
.
这是我 SandwichDB.app 上的代码:
public static String[] getVegetableInfo()
{
ArrayList<String> vegsList = new ArrayList<>();
try (BufferedReader in =
new BufferedReader(
new FileReader("vegetable.txt")))
{
String line = in.readLine();
while (line != null)
{
String[] elems = line.split("~");
vegsList.add(elems[0] + " $" + elems[1]);
line = in.readLine();
}
vegsList.add("Quit Vegetable Section");
}
catch(IOException e)
{
System.out.println(e);
return null;
}
String[] vegetableInfo = vegsList.toArray(new String[]{});
return vegetableInfo;
}