所以我在第一学期就需要完成一个餐厅订单项目。 一切都进展顺利,但我对这个项目的规定感到困惑:
III。一旦当前用餐者的选择被显示,则显示他们选择的所有项目的列表。如果他们在每个菜单上选择第一个选项,则不显示该项目:无选择。
没有选择是数组的索引0。我怎样才能排除索引0?
这是4个while while循环中的一个,用户输入他们的选择并验证数据,如果有帮助,我完全被卡住了。谢谢!
System.out.println("Please select one item from the beverages menu.");
do {
for(int j = 0; j < beveragesArray.length; j++) {
System.out.println((j + 1) + ". " +
beveragesArray[j] + " $" + beveragesPriceArray[j]);
}
System.out.println("Please enter your selection #: ");
beverageSelection = input.nextInt();
if(beverageSelection < 1 || beverageSelection > beveragesArray.length) {
System.out.println("We don't serve that, what else would you like?");
badDataFlag = true;
} else {
badDataFlag = false;
accumulator += beveragesPriceArray[beverageSelection - 1];
break;
}
} while (badDataFlag = true);
这是项目的输出结果。
Please select one item from the Appetizer menu.
1. **No selection** $0.00
2. Deep Fried Calamari $7.50
3. Soup du Jour $4.99
4. Garden Salad $3.99
5. Garlic Bread $4.50
Please enter your selection #: 6
INVALID SELECTION. Please enter your selection #: 1
Please select one item from the Entrée menu.
1. **No selection** $0.00
2. Rib-Steak $15.95
3. Fettuccini Alfredo $11.25
4. Pan-Fried Sole $17.95
5. Mediterranean Platter $13.50
6. Vegetarian Lasagna $9.00
Please enter your selection #: 4
Please select one item from the Dessert menu.
1. **No selection** $0.00
2. Ice Cream Sundae $2.95
3. Cheesecake $5.00
4. Chocolate Truffle Cake $6.00
5. Raspberry Mousse $4.50
Please enter your selection #: 5
Please select one item from the Beverage menu.
1. **No selection** $0.00
2. Water $0.00
3: Juice
4. Pop $2.00
5. Milk $2.00
6. Coffee $1.75
7. Tea $1.75
Please enter your selection #: 6
Thank you, Evelyn. Your order consists of:
Pan-Fried Sole
Raspberry Mousse
Tea
答案 0 :(得分:0)
虽然有很多方法可以解决这个问题,但最简单的方法是添加&#34;项目1无选择&#34;正如您当前在打印输出时那样,只需使用if语句。如果值为第1项,则不打印任何内容,否则打印它是什么。
虽然您的相应代码没有发布,但它可能就像这样简单。
if ( itemSelectedindex > 0 ){
// print something tasty
}
答案 1 :(得分:0)
你可以声明String,以累积顺序,而不是添加index = 0的项目 从所有do-while循环中声明这个字符串,可能是main()
中的第一行String customerOrder = "";
并在此位置添加新的if语句
if(beverageSelection < 1 || beverageSelection > beveragesArray.length) {
System.out.println("We don't serve that, what else would you like?");
badDataFlag = true;
} else {
badDataFlag = false;
accumulator += beveragesPriceArray[beverageSelection - 1];
//NEW CODE//
if(beverageSelection != 1){//1. **No selection** $0.00
customerOrder += beveragesArray[beverageSelection - 1] + "\n";
}
//NEW CODE//
break;
}
最后,在挑选完所有项目后,
System.out.println("Thank you, Evelyn. Your order consists of:");
System.out.println(customerOrder );