如何在数组中查找元素

问题描述 投票:0回答:1

我目前有一个编程任务。在分配中,我必须有两个数组来存储商店库存数据。我使用第一个数组供Manager访问。我使用第二个数组供收银员访问。

经理最多可以添加15个项目的代码,名称,价格和数量。收银员可以添加任何物品的代码和数量。如果管理器的数组中不存在的代码,则程序将自动退出。

两个数组列表的数组元素顺序不同。因此,如何确定出纳员输入的商品代码确实存在于经理的阵列中?

这是我声明数组的方式:

        String[] stockItemCode = new String[15];
        String[] stockItemName = new String[stockItemCode.length];
        int[] stockItemQuantity = new int[stockItemCode.length];
        double[] stockItemPrice = new double[stockItemCode.length];

        String[] saleItemCode = new String[stockItemCode.length];
        String[] saleItemName = new String[stockItemCode.length];
        int[] saleItemQuantity = new int[stockItemCode.length];
        double[] saleItemPrice = new double[stockItemCode.length];

管理员输入项目的方式:

// 3- The manager:
// 3.1- The manager has to login to add the items to the inventory list. The username and password are "Manager" and "Man_2020" respectively.
            if (username.equals(managerUsername) && password.equals(managerPassword))
            {
// 3.2- Once the manager is logged in, he will enter the item's code, name, quantity, and price. 
                do
                {
                    System.out.println("Please enter item code: ");
                    stockItemCode[stockItemLimit] = sc.next();

                    System.out.println("Please enter item name: ");
                    stockItemName[stockItemLimit] = sc.next();

                    System.out.println("Please enter item quantity (numbers only): ");
                    stockItemQuantity[stockItemLimit] = sc.nextInt();

                    System.out.println("Please enter item price (numbers only): ");
                    stockItemPrice[stockItemLimit] = sc.nextDouble();

                    stockItemLimit++;
// 3.3- After entering the above information for each item, the program prompts the manager to continue or logout of his account. He has to use 'L' or 'S' to sign-out.
                    System.out.println("Would you like to stop the program? Entering S or L will stop the program");
                    logoutPrompt = sc.next().charAt(0);
                }
                while(!(logoutPrompt == 'L' || logoutPrompt == 'S'));
            }

这就是我试图比较数组元素的方式(我知道这是错误的,但是我不知道为什么。我对编程非常陌生。]]

// 4- The sales employee:
// 4.1- The sales employee logs in to scan the sold items and print the receipt. The username and password are "Sales" and "Sale_2020" respectively.
            else if (username.equals(salesUsername) && password.equals(salesPassword))
            {
                i = 0;
// 4.2- The sales employee, enters the *code* and the *quantity* of the sold item.
                do
                {
                    System.out.println("Please enter item code: ");
                    saleItemCode[i] = sc.next();

                    System.out.println("Please enter item quantity (numbers only): ");
                    saleItemQuantity[i] = sc.nextInt();

                    saleItemName[i] = stockItemName[i]; //This is the problem
                    saleItemPrice[i] = stockItemPrice[i]; //This is the problem

// 4.3- The program calculates the total price of the transaction, by using the quantity and the price of the sold items.
                    if(saleItemCode[i] == stockItemCode[i])
                    {
                        saleItemPrice[i] = stockItemPrice[i] * saleItemQuantity[i];
// 4.4- The program has to deduct the sold quantity of the items from the stock list.
                        stockItemQuantity[i] = stockItemQuantity[i] - saleItemQuantity[i];
// 4.5- After entering each item, the employee is prompted to continue adding the item to the receipt, or print it (by using 'P').
                        System.out.println("Would you like to print the receipt by entering P? Enter anything else to continue adding items.");
                        logoutPrompt = sc.next().charAt(0);
                    }

                    else
                    {
// If the code is entered incorrectly, the program skips to 4.5.
                        System.out.println("Would you like to print the receipt by entering P? Enter anything else to continue adding items.");
                        logoutPrompt = sc.next().charAt(0);
                    }

                    i++;

                }
                while(logoutPrompt != 'P');

[此外,在收银机循环中,每当我尝试打印股票项目的信息时,即使明确存在,arrayElements也会返回null,就好像它们不存在一样。

for(i = 0; i <= stockItemLimit; i++) //COME HERE
{
int indexNumb = i+1;
System.out.println(indexNumb + "\t" + saleItemCode[i] + "\t" + saleItemName[i] + "\t" + "$" + saleItemPrice[i] + "\t" + saleItemQuantity[i]);
}

将不胜感激。

我目前有一个编程任务。在分配中,我必须有两个数组来存储商店库存数据。我使用第一个数组供Manager访问。我使用...

java
1个回答
0
投票

您通过遍历所有元素并进行比较来找到未排序数组中的元素。

© www.soinside.com 2019 - 2024. All rights reserved.