简体   繁体   English

"For循环多次打印数组中的元素"

[英]For Loop printing an element from an array more than once

For this question, I'm trying to make a code which filters out items which have already been purchased from the list.对于这个问题,我正在尝试编写一个代码来过滤掉已经从列表中购买的项目。 But my for loop is printing all the element from the Item List more than once while also simultaneously ignoring the if condition.但是我的 for 循环不止一次地打印项目列表中的所有元素,同时也忽略了 if 条件。

The input for my scanner is this:我的扫描仪的输入是这样的:

5 apples oranges bananas peaches grapes oranges peaches DONE 5 个 苹果 橙子 香蕉 桃子 葡萄 橙子 桃子 完成

<\/blockquote>

The integer in the beginning is the size of my itemList array.开头的整数是我的 itemList 数组的大小。 Items which are repeated are considered as purchased.重复的项目被视为已购买。 So oranges and peaches are purchased in this situation.所以在这种情况下购买橙子和桃子。

Here is my code:这是我的代码:

 Scanner in = new Scanner(System.in); \/\/ 5 apples oranges bananas peaches grapes oranges peaches DONE int n = in.nextInt(); String[] itemList = new String[n]; String bought; String[] itemPurchased = new String[0]; for(int i = 0; i < itemList.length; i++) { String item = in.next(); itemList[i] = item; } if(in.hasNext()) { bought = in.nextLine(); itemPurchased = bought.split(" "); } for(int i = 0; i < itemList.length; i++) { for(int j = 0; j < itemPurchased.length; j++) { if(!itemList[i].equals(itemPurchased[j])) { System.out.println(itemList[i]); } } }<\/code><\/pre>

This is the output:这是输出:

apples apples apples apples oranges oranges oranges bananas bananas bananas bananas peaches peaches peaches grapes grapes grapes grapes苹果 苹果 苹果 橘子 橘子 橘子 香蕉 香蕉 香蕉 桃子 桃子 桃子 葡萄 葡萄 葡萄 葡萄

<\/blockquote>

As you can see the output repeats all the elements in my itemList array 4 times while also including the items repeated from the input.如您所见,输出将我的 itemList 数组中的所有元素重复 4 次,同时还包括从输入中重复的项目。 How do I make it print all the element once excluding the items that have already been purchased?除了已购买的商品外,如何让它打印所有元素?

"

In the loop在循环

    for(int i = 0; i < itemList.length; i++) {
        for(int j = 0; j < itemPurchased.length; j++) {
            if(!itemList[i].equals(itemPurchased[j])) {
                System.out.println(itemList[i]);
            }
        }
    }

The logic of your code is worng.您的代码逻辑已磨损。 Just replace the last part with:只需将最后一部分替换为:

for(int i = 0; i < itemList.length; i++) {
    boolean alreadyBought = false;
    for(int j = 0; j < itemPurchased.length; j++) {
        if(itemList[i].equals(itemPurchased[j])) {
           alreadyBought = true;
        }
    }
    if(!alreadyBought) {
        System.out.println(itemList[i]);
    }
}

In your code, you check every element in itemList with all the elements in itemPurchased and in case it does not exist then print.在您的代码中,您检查itemPurchased中的每个元素以及itemList中的所有元素,如果它不存在则打印。 This means if your itemPurchased contains n elements, then you will print n time non-bought elements这意味着如果您的itemPurchased包含n元素,那么您将打印n次未购买的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM