简体   繁体   English

如何确定字符串是否不是数组的一部分并计算出现次数?

[英]How to determine if a String is not a part of an array and count occurrences?

I have a program that takes in a series of items and prices as well as the number of customers and what/how many of the items available was purchased.我有一个程序可以接收一系列商品和价格以及客户数量以及购买了哪些/多少可用商品。 I was able to figure out the inputs, but I want to know how to print which items were not purchased at all for all the customers, and how many of each item was purchased.我能够计算出输入,但我想知道如何为所有客户打印哪些商品根本没有购买,以及每件商品购买了多少。

This is what I have so far:这是我到目前为止所拥有的:

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);

        // Integer count of number of items in the store
        int count = scan.nextInt();

        // Create an array to store names and prices of each item
        String[] itemName = new String[count];
        double[] itemPrice = new double[count];

        for (int i=0; i<count; i++) {
            // Scan name of each item and price
            itemName[i] = scan.next();
            itemPrice[i] = scan.nextDouble();
        }

        // Integer count for the number of customers
        int numCustomers = scan.nextInt();
        String[] nameF = new String[numCustomers];
        String[] nameL = new String[numCustomers];
        double[] costs = new double[numCustomers];

        for (int j=0; j<numCustomers; j++) {
            // First and last name of each customer
            nameF[j] = scan.next();
            nameL[j] = scan.next();

            //Number of items bought
            int numItems = scan.nextInt();
            String[] customerItems = new String[numItems];

            for (int k=0; k<numItems; k++) {
                // For each number of items bought, name and quantity
                int numItemBought = scan.nextInt();
                String nameOfItem = scan.next();

                for (int i=0; i<count; i++) {
                    if (nameOfItem != itemName[i]) {
                        String msg = "No one bought " + nameOfItem;
                        System.out.println(msg);
                    }
                }
            }
        }

    }

You can create two HashMap s for storing the number of times an item was bought and the number of people that bought it, as well as a HashSet on each iteration so that the count of people buying an item will be correct even if the same person bought multiple of the same item.您可以创建两个HashMap来存储某件商品的购买次数和购买人数,并在每次迭代时创建一个HashSet ,这样即使是同一个人,购买商品的人数也是正确的购买了多个相同的项目。 The statistics should only be printed at the end, after all the purchases have been accounted for.统计数据应该只在最后打印,在所有购买都已计算在内之后。

final Map < String, Integer > numBought = new HashMap < > ();
final Map < String, Integer > peopleBought = new HashMap < > ();
for (int j = 0; j < numCustomers; j++) {
    // First and last name of each customer
    nameF[j] = scan.next();
    nameL[j] = scan.next();

    // Number of items bought
    final Set < String > vis = new HashSet < > ();
    int numItems = scan.nextInt();
    for (int k = 0; k < numItems; k++) {
        // For each number of items bought, name and quantity
        int numItemBought = scan.nextInt();
        String nameOfItem = scan.next();
        numBought.merge(nameOfItem, numItemBought, Integer::sum); // increase quantity of the item
        if (vis.add(nameOfItem))
            peopleBought.merge(nameOfItem, 1, Integer::sum); // increment number of people who bought this item
    }
}
for (final String item: itemName) {
    final Integer num = numBought.get(item);
    if (num == null) {
        System.out.println("No one bought " + item);
    } else {
        System.out.println(peopleBought.get(item) + " bought " + num + " " + item);
    }
}

Demo演示

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

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