V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
muunala10221
V2EX  ›  编程

请问一个比较简单的问题

  •  
  •   muunala10221 · 2021-06-21 18:17:35 +08:00 · 862 次点击
    这是一个创建于 1011 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如下面这段代码
    if (show1 && show2 && show3) {
    return progress1 === 'success' && progress2 === 'success' && progress3 === 'success'
    } else if (show1 && show2) {
    return progress1 === 'success' && progress2 === 'success'
    }...
    我有什么更好的办法去写这个判断吗,像上文那种写法我还要判断 show2 && show3 或者 show1 && show3 等...
    muunala10221
        1
    muunala10221  
    OP
       2021-06-21 18:21:47 +08:00
    另外是建立在不改变数据结构的基础上哈,这个牵连太广了
    uselessVisitor
        2
    uselessVisitor  
       2021-06-21 18:59:40 +08:00
    show 和 progress === 'success'相关联,那么他俩&&一下,然后 return 不行吗。。
    AoEiuV020
        3
    AoEiuV020  
       2021-06-21 19:04:57 +08:00 via Android
    !(s1&&!p1 || s2&&!p2 || s3&&!p3)
    uselessVisitor
        4
    uselessVisitor  
       2021-06-21 20:36:04 +08:00
    Java 这样写。。在群里大佬帮助下
    public static boolean test2(List<Pair<Boolean,String>> conditions,int originSize){
    //排除所有 show 为 false 的
    conditions.removeIf(booleanStringPair -> !booleanStringPair.getKey());
    int failShow = originSize - conditions.size();
    if (failShow > 1){
    return false;
    }
    //现在都是 show 为 true 的 直接返回对应的&&
    return conditions.stream()
    .map(pair -> pair.getValue().equals("1"))
    .reduce(true, (a, b) -> a && b);
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:08 · PVG 15:08 · LAX 00:08 · JFK 03:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.