gpt4 book ai didi

java - 如何查找数组中的元素

转载 作者:行者123 更新时间:2023-12-01 16:53:31 25 4
gpt4 key购买 nike

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

经理可以添加最多 15 件商品的代码、名称、价格和数量。收银员可以添加任何商品的代码和数量。如果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');

此外,每当我尝试在出纳循环中打印库存商品的信息时,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]);
}

所有帮助将不胜感激。

最佳答案

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

 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 数组,这就是为什么您得到空内容的原因。

关于java - 如何查找数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com