You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java新手售票找零if语句问题:收100元无50元仅3张25元如何处理

问题解决思路

你现有代码的100元找零逻辑只覆盖了「同时有50和25」的场景,缺少了「无50但有至少3张25」的判断分支,调整方案如下:


方案1:用计数变量优化(更推荐,逻辑清晰易维护)

直接用两个变量分别统计25、50的持有数量,比用ArrayList存所有纸币再查找/删除的效率更高,也更易懂:

public static void line(Integer... people) {
    // 分别统计收银盒里25、50的数量,100没法用来找零不用统计
    int count25 = 0;
    int count50 = 0;
    
    for (int money : people) {
        if (money == 25) {
            count25++;
            System.out.println("Here is your ticket!");
        } else if (money == 50) {
            if (count25 >= 1) {
                count25--;
                count50++;
                System.out.println("Here is your ticket!");
            } else {
                System.out.println("Sorry, I haven't odd money for you!");
            }
        } else if (money == 100) {
            // 优先用1张50+1张25找零,保留更多25应对后续50的找零需求
            if (count50 >=1 && count25 >=1) {
                count50--;
                count25--;
                System.out.println("Here is your ticket!");
            // 没有50的情况下,判断是否有至少3张25
            } else if (count25 >=3) {
                count25 -=3;
                System.out.println("Here is your ticket!");
            } else {
                System.out.println("Sorry, I haven't odd money for you!");
            }
        }
    }
}

方案2:基于你原有ArrayList写法修改

如果你不想改动原有存储结构,只需要补充100元的判断分支,用Collections.frequency统计25的数量即可,同时修正你原有代码漏存收到的50、100的问题:

import java.util.ArrayList;
import java.util.Collections;

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));
            cashbox.add(money); // 补上存入收到的50的逻辑
            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));
            cashbox.add(money); // 补上存入收到的100的逻辑
            continue;
        // 新增分支:没有50,但是25的数量≥3
        } else if (money == 100 && !cashbox.contains(50) && Collections.frequency(cashbox, 25) >=3) {
            System.out.println("Here is your ticket!");
            // 移除3张25
            for (int i = 0; i < 3; i++) {
                cashbox.remove(cashbox.indexOf(25));
            }
            cashbox.add(money);
            continue;
        }
        else {System.out.println("Sorry, I haven't odd money for you!");}
    }
}

补充说明

100元找零优先用50+25的组合是最优策略:因为50只能用来给100元找零,而25可以同时给50、100元找零,优先消耗50能降低后续缺零钱的概率。

内容的提问来源于stack exchange,提问作者MKos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 02:00:01