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

如何高效简化Java中的if-else代码?附现有实现代码

优化多条件if-else的几种方案

针对你这段判断吉普尼线路对应地点的代码,有几种简洁高效的优化方式,核心思路是把硬编码的条件判断转化为键值映射,或者先合并重复的前置条件:

方案1:合并相同数字的判断,减少重复代码

原代码里大量重复判断number == 12,可以先把这个判断抽出来,在内部处理不同字母的分支,这样能减少冗余:

System.out.println("Please type the letter in Capital only.");
System.out.println("Enter the jeepney's number");
number = scan.nextInt();
System.out.println("Enter the jeepney's letter");
letter = scan.next().charAt(0);

String location = null;
if (number == 12) {
    switch (letter) {
        case 'A' -> location = "Carbon";
        case 'C' -> location = "Panganiban";
        case 'F' -> location = "Taboan";
        case 'G', 'I' -> location = "Mabolo";
        case 'L' -> location = "Labangon";
    }
} else if (number == 13 && letter == 'C') {
    location = "Talamban";
} else if (number == 14 && letter == 'D') {
    location = "Capitol";
}

if (location != null) {
    System.out.println("Your usual location is " + location);
} else {
    System.out.println("No record in the database");
}

这里用了Java 14+的switch表达式,比传统switch更简洁;如果是低版本Java,换成传统switch语句即可。

方案2:用Map存储映射关系(最推荐)

把所有(数字+字母)的组合和对应地点提前存在Map里,直接通过键查找结果,彻底消除if-else:

方式一:用字符串拼接当键(兼容所有Java版本)

// 提前初始化映射(可以放在类的成员变量位置,避免每次创建)
private static final Map<String, String> JEEPNEY_LOCATIONS = new HashMap<>();
static {
    // 12开头的线路
    JEEPNEY_LOCATIONS.put("12A", "Carbon");
    JEEPNEY_LOCATIONS.put("12C", "Panganiban");
    JEEPNEY_LOCATIONS.put("12F", "Taboan");
    JEEPNEY_LOCATIONS.put("12G", "Mabolo");
    JEEPNEY_LOCATIONS.put("12I", "Mabolo");
    JEEPNEY_LOCATIONS.put("12L", "Labangon");
    // 其他线路
    JEEPNEY_LOCATIONS.put("13C", "Talamban");
    JEEPNEY_LOCATIONS.put("14D", "Capitol");
}

// 业务代码部分
System.out.println("Please type the letter in Capital only.");
System.out.println("Enter the jeepney's number");
int number = scan.nextInt();
System.out.println("Enter the jeepney's letter");
char letter = scan.next().charAt(0);

// 拼接键
String key = number + String.valueOf(letter);
String location = JEEPNEY_LOCATIONS.get(key);

if (location != null) {
    System.out.println("Your usual location is " + location);
} else {
    System.out.println("No record in the database");
}

方式二:用自定义记录类当键(Java 16+,更优雅)

如果用Java 16及以上,可以用record定义复合键,避免字符串拼接的潜在问题:

// 定义复合键记录
record JeepneyKey(int number, char letter) {}

// 初始化映射
private static final Map<JeepneyKey, String> JEEPNEY_LOCATIONS = new HashMap<>();
static {
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'A'), "Carbon");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'C'), "Panganiban");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'F'), "Taboan");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'G'), "Mabolo");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'I'), "Mabolo");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(12, 'L'), "Labangon");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(13, 'C'), "Talamban");
    JEEPNEY_LOCATIONS.put(new JeepneyKey(14, 'D'), "Capitol");
}

// 业务代码
System.out.println("Please type the letter in Capital only.");
System.out.println("Enter the jeepney's number");
int number = scan.nextInt();
System.out.println("Enter the jeepney's letter");
char letter = scan.next().charAt(0);

String location = JEEPNEY_LOCATIONS.get(new JeepneyKey(number, letter));
if (location != null) {
    System.out.println("Your usual location is " + location);
} else {
    System.out.println("No record in the database");
}

额外提示

原代码里有个拼写错误:Your usul location应该是Your usual location,优化的时候记得修正。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 15:50:38