【问题标题】:Problems with the task about if statement关于if语句的任务问题
【发布时间】:2021-10-24 14:06:21
【问题描述】:

我是Java初学者,请对我好一点。

我有一个任务:排队的人要买票。他们有钱三小费:25,50,100。门票费用 - 25。如果收银员有零钱 - 他会给票,如果没有 - 不。

如果钱箱里有小费25(25+25+25)的钱足够了,但是没有人有小费50,收银员从别人那里拿了100怎么办?

我的代码:

public static void line(Integer... people) {
    ArrayList<Integer> cashbox = new ArrayList<>();
    
    for (int money : people) {
        if (money == 25) {
            cashbox.add(money);
            System.out.println("Here is your ticket!");
            continue;
        } else if (money == 50 && cashbox.contains(25)) {
            System.out.println("Here is your ticket!");
            cashbox.remove(cashbox.indexOf(25));
            continue;
        } else if (money == 100 && cashbox.contains(50) && cashbox.contains(25)) {
            System.out.println("Here is your ticket!");
            cashbox.remove(cashbox.indexOf(50));
            cashbox.remove(cashbox.indexOf(25));
            continue;
        }
        else {System.out.println("Sorry, I haven't odd money for you!");}
    }
}

【问题讨论】:

    标签: java arrays if-statement arraylist


    【解决方案1】:

    目前还不清楚为什么需要ArrayList 作为钱箱,可以为每种类型的钞票替换为三个计数器,并检查/修改这些计数器的值。使用contains/indexOf 在列表中进行多次搜索远非最佳。

    类似地,这些计数器可能被组织成一张地图,其中货币类型是关键,每张纸币的数量是价值。

    另外,值得一提的是,初始代码在售票时没有添加 50 和 100 的注释:)

    基于地图的钱箱示例如下:

    public static void line(int ... people) {
        Map<Integer, Integer> cashbox = new HashMap<>();
        
        for (int money : people) {
            boolean ok = false;
            String change = "";
            
            switch (money) {
                case 25: 
                    ok = true; break;
                case 50: 
                    ok = cashbox.getOrDefault(25, 0) > 0;
                    if (ok) {
                        cashbox.put(25, cashbox.get(25) - 1);
                        change = "25";
                    }
                    break;
                case 100:
                    ok = cashbox.getOrDefault(50, 0) > 0 && cashbox.getOrDefault(25, 0) > 0 || cashbox.getOrDefault(25, 0) > 2;
                    if (ok) {
                        if (cashbox.getOrDefault(50, 0) > 0) {
                            cashbox.put(50, cashbox.get(50) - 1);
                            cashbox.put(25, cashbox.get(25) - 1);
                            change = "50+25 = 75";
                        } else {
                            cashbox.put(25, cashbox.get(25) - 3);
                            change = "3*25 = 75";
                        }
                    }
                    break;
            }
            if (ok) {
                cashbox.compute(money, (pay, count) -> count == null ? 1 : count + 1);
                System.out.println("Here is your ticket" + (change.isEmpty() ? "" : " and change " + change) + "!");
            } else {
                System.out.println("Sorry, I haven't odd money for your " + money);
            }
        }
        // remove "empty" counters
        cashbox.entrySet().removeIf(e -> e.getValue() == 0);
        System.out.println("cashbox: " + cashbox);
    }    
    

    测试和输出

    line(25, 25, 50, 100, 50, 25, 25, 100, 25, 100);
    
    Here is your ticket!
    Here is your ticket!
    Here is your ticket and change 25!
    Here is your ticket and change 50+25 = 75!
    Sorry, I haven't odd money for your 50
    Here is your ticket!
    Here is your ticket!
    Sorry, I haven't odd money for your 100
    Here is your ticket!
    Here is your ticket and change 3*25 = 75!
    cashbox: {100=2}
    

    【讨论】:

    • 哇,太酷了))谢谢)老实说,我对“地图”一无所知,但我是立方体的初学者?
    【解决方案2】:

    您要做的是检查您是否至少有 3 张 25 元的钞票。您可以通过为该值过滤您的钱箱 ArrayList 并检查它是否包含 >= 3 个结果来做到这一点。

    cashbox.stream().filter(money -> money == 25).count() >= 3
    

    所以最后你需要做的是在你的代码中添加另一个 else if:

    else if (money == 100 && cashbox.stream().filter(money -> money == 25).count() >= 3) {
        System.out.println("Here is your ticket!");
        cashbox.remove(cashbox.indexOf(25));
        cashbox.remove(cashbox.indexOf(25));
        cashbox.remove(cashbox.indexOf(25));
        continue;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多