繁体   English   中英

关于 if 语句的任务问题

[英]Problems with the task about if statement

我是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!");}
    }
}

你要做的是检查你是否至少拿到了 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;
}

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

类似地,这些柜台可能被组织成 map,其中货币类型是键,每张纸币的金额是值。

此外,值得一提的是,初始代码在售票时不会添加 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);
}    

测试和 output

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}

暂无
暂无

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

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