【问题标题】:How to find an element in an array [duplicate]如何在数组中查找元素[重复]
【发布时间】:2020-08-21 10:43:41
【问题描述】:

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

经理最多可以添加 15 件商品的代码、名称、价格和数量。 收银员可以添加任何项目的代码和数量。如果manager的数组中不存在该代码,程序会自动退出。

两个数组列表的数组元素顺序不同。那么如何确保收银员输入的商品代码确实存在于Manager的数组中呢?

这就是我声明数组的方式:

        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');

此外,每当我在 Cashier's 循环中尝试打印股票项目的信息时,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]);
}

我们将不胜感激。

【问题讨论】:

  • 我们只研究了一维数组。所以我们不允许使用二维数组,如果这就是你的意思的话。
  • 我以为它们是一样的。我现在就改正这个错误。谢谢。

标签: java


【解决方案1】:

通过遍历所有元素并比较每个元素,您可以在未排序的数组中找到一个元素。

 int stockitemindex = -1;
 for(int searchindex = 0; searchindex <= stockItemLimit; searchindex++) {
     if (stockItemCode[searchindex].equals(saleItemCode[i])) {
          stockitemindex = searchindex;
          break;
     }
 }

从您的描述中不清楚为什么 stockitem 和 saleitem 的数组具有相同的大小。甚至不清楚您是否需要一个用于 saleitem 的数组。一个 String 代码和一个 int 数量似乎就足够了。

同样在问题结束时的循环中,您访问的是 saleitem 数组,而不是 stockitem 数组,这就是您获得空内容的原因。

【讨论】:

  • 感谢第一部分的提示,感谢您的帮助,我似乎已经解决了这个问题。然而,关于这个:“同样在你的问题结束时的循环中,你访问的是 saleitem 数组,而不是 stockitem 数组,这就是你得到空内容的原因。” ,我目前正在尝试访问 stockItem 数组,但又遇到了同样的错误。为什么会这样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2014-11-10
  • 2011-06-06
相关资源
最近更新 更多