首页 > 解决方案 > 爪哇 || varArr[i] = varInt 后数组元素设置为 0

问题描述

我希望我能充分描述这一点。

该程序的作用:

程序的这一部分检查哪些商品可以购买,显示这些商品,然后询问用户他们想要购买哪些商品。然后它询问他们想要购买多少该项目并将该数字添加到整数数组中,其中该数组索引对应于项目名称的索引(因此名称和数量存储在两个数组中,但按索引对齐)。一旦整数被输入到数组中,循环再次开始询问要购买的物品,直到用户输入“完成”,这将停止循环。另一种方法使用该整数数组来生成收据。

我的问题:

当需要将整数输入数组时,我会立即将该数组打印到控制台,并得到这个结果。

比如说,第一个循环将“10”添加到索引“1”。

element at index '0' = 0
element at index '1' = 10
element at index '2' = 0
element at index '3' = 0

然后说,第二个循环将“20”添加到索引“3”。

element at index '0' = 0
element at index '1' = 0
element at index '2' = 0
element at index '3' = 20

我的问题是,为什么索引“1”恢复为“0”?

我的期望:

我希望整数继续被添加到数组中。使用上面的例子,一旦第二个循环完成,我期望得到以下结果。

element at index '0' = 0
element at index '1' = 10
element at index '2' = 0
element at index '3' = 20

编码:

编辑:添加了此块的其余部分以满足最小的、可重现的示例。
do {
    System.out.println(saleMenu);
    String[] availArray = new String[stockName.length];
    for (int i = 0; i < stockLevel.length; i++) {
        if (stockLevel[i] > 0) {
            availArray[i] = stockName[i];
        }
    }
    for (int i = 0; i < availArray.length; i++) {
        System.out.println(availArray[i]);
    }
    System.out.println("\nType 'finish' to generate a receipt.");
    soldItems = new int[availArray.length];
    System.out.println(saleMenu2);                                      // Display menu.
    String input = console.nextLine().toLowerCase();                    // Get input.
    if (!(input.contains("finish"))) {                                  // If input not "finish".
        for (int i = 0; i < availArray.length; i++) {                   // Loop through availableArray
            if (availArray[i].equals(input)) {                          // If stock present.
                available = true;                                       // Set available flag.
                index = i;                                              // Grab the index.
            }
        }
        if (available == false) {                                       // If input not available.
            System.out.print("That is not an option. Try again.\n");    // Display error.
        }
        if (available == true) {                                        // If input available.
            available = false;                                          // Reset available flag.
            System.out.println("How many would you like to purchase?"); // Display message.
            amountInput = console.nextInt();                            // Get input.
            console.nextLine();                                         // Capture stray \n
            if (amountInput < 0) {                                      // Is input < 0.
                System.out.println("\nSorry, the number must be positive.");    // Display error.
            } else
                if (!(amountInput == 0)) {                              // If input not 0.
                    soldItems[index] += amountInput;                    // Add input to array at 
                                                                        // 'index' grabbed above.
                }
                else
                    System.out.println("\nNothing added.");             // If input 0, display error.
            for (int i = 0; i < soldItems.length; i++) {                                // Debugging
                System.out.println("sldItems at index '" + i + "' " + soldItems[i]);    // Debugging.
            }
        }
    } else
        finishedSale = true;
} while (finishedSale == false);
processSale(soldItems, stockLevel);
System.out.print(continueOption);
continueYN = console.nextLine().toLowerCase().charAt(0);
if (continueYN == 'y') {
    mainMenuSelect = 0;
}
break;

标签: java

解决方案


do {
    // [...]
    soldItems = new int[availArray.length];
    // [...]
} while (finishedSale == false)

您在每次迭代时重置数组。每次购买后,数组都会被擦除并重新实例化——包含零。将实例移到循环外。

顺便说一句,finishedSale == false!finishedSale.


推荐阅读