gpt4 book ai didi

java - 排除数组的索引 0 作为输出选项?

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:21 24 4
gpt4 key购买 nike

所以我正在第一学期,需要完成一个餐厅订单项目。一切都很顺利,但我对这个项目规定很感兴趣:

三.当前用餐者做出选择后,将显示他们选择的所有项目的列表。如果他们选择每个菜单上的第一个选项,则不会显示该项目:无选择。

没有选择数组的索引 0。如何只排除索引 0?

这是 4 个 do 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

最佳答案

虽然有很多方法可以解决这个问题,但最简单的方法是像当前一样添加“Item 1 No Selection”,然后在打印输出时,只需使用 if 语句即可。如果值为 Item 1,则不打印任何内容,否则,打印它的内容。

虽然您的相应代码没有发布,但它可能就是这么简单。

if ( itemSelectedindex > 0 ){
// print something tasty
}

关于java - 排除数组的索引 0 作为输出选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26831097/

24 4 0
文章推荐: java - Android 主机名未得到验证。如何允许所有主机?
文章推荐: c++ - WM_MOUSEMOVE 不适用于 Direct3D 中的 FPS 相机实现
文章推荐: 具有流畅三列布局的 css 图像悬停动画
文章推荐: Jquery - 将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com