superhreo123
V2EX  ›  问与答

有 A、B 两个条件,至少有一个为真的情况下继续执行相应的逻辑。若同时为真,只执行同时为真的逻辑。这样的逻辑怎样实现才算优雅

  •  
  •   superhreo123 · May 10, 2021 · 1373 views
    This topic created in 1831 days ago, the information mentioned may be changed or developed.
    if A == true {
    //执行 A 的逻辑
    }

    if B == true {
    //执行 B 的逻辑
    }

    if A == true && B == true {
    //执行其他逻辑
    }
    7 replies    2021-05-11 08:55:06 +08:00
    zm8m93Q1e5otOC69
        1
    zm8m93Q1e5otOC69  
       May 10, 2021
    这不就挺清晰的,能看明白就行
    superhreo123
        2
    superhreo123  
    OP
       May 10, 2021
    @beichenhpy 上面那种写法,如果 A 和 B 同时为 true,就会执行 A 的逻辑和 AB 逻辑。是不对的
    siweipancc
        3
    siweipancc  
       May 10, 2021 via iPhone   ❤️ 1
    你把第三个判断提升到顶然后里边塞个 return 或已处理标志不就好了,c 语言上机课的经典例题 orz
    msg7086
        4
    msg7086  
       May 10, 2021 via Android
    先写#3 然后 else if #1 else if #2 不就行了。
    3 个分支必然有 3 个 block,又不能省代码。
    zxCoder
        5
    zxCoder  
       May 10, 2021
    @superhreo123 先判 a && b 的逻辑
    revlis7
        6
    revlis7  
       May 10, 2021
    switch(true)

    case A && B
    break;
    case A
    break;
    case B
    break;
    treblex
        7
    treblex  
       May 11, 2021
    if A == true && B == true {
    //执行其他逻辑
    return
    }

    if A == true {
    //执行 A 的逻辑
    return
    }

    if B == true {
    //执行 B 的逻辑
    return
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   937 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 19:57 · PVG 03:57 · LAX 12:57 · JFK 15:57
    ♥ Do have faith in what you're doing.