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

标签 java arrays loops

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

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

没有选择数组的索引 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/

相关文章:

java - 使用 Java 正则表达式模式解析字符串?

javascript - Appium selenium 字段中的明文

c# - 将 int[] 数组转换为 ushort[] 数组

java - 如何仅使用 Scanner 读取文件并将每个句子存储在 arrayList 中?

java - 如何反转 while 循环生成的一系列数字的顺序?

java - 字节数组的验证

java - 编写一个java数组并得到太多输出

ruby - 比较数组中多个散列的值

c - 以下程序打印的输出是什么?如何评估if条件表达式?

Java:搜索未排序的整数数组并返回有多少个数字更大