V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
esolve
V2EX  ›  问与答

两个变量中,仅仅有一个是 null,如何简便写法?

  •  
  •   esolve · 2017-05-22 10:41:19 +08:00 · 1629 次点击
    这是一个创建于 2531 天前的主题,其中的信息可能已经有所发展或是发生改变。

    if((a==null && b!=null) || (a!=null) && b==null) 有啥简便写法?

    10 条回复    2017-05-22 22:04:54 +08:00
    am241
        1
    am241  
       2017-05-22 10:44:16 +08:00 via Android
    分语言
    c 可以用真值异或,或者真值相加=1

    某些其他需要就老老实实正常写吧
    drush
        2
    drush  
       2017-05-22 10:46:17 +08:00
    php 有个 xor operator

    $a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
    esolve
        3
    esolve  
    OP
       2017-05-22 10:47:03 +08:00
    @drush java 呢
    drush
        4
    drush  
       2017-05-22 10:48:07 +08:00
    M3oM3oBug
        5
    M3oM3oBug  
       2017-05-22 11:02:18 +08:00 via Android
    这就跟 2 块硬币的正反是一样的,同时为正是一种情况,一正一反是有两种情况的呀,想写就自己弄个方法返回特征码,以后直接调用方法那就是一行语句了
    princelai
        6
    princelai  
       2017-05-22 12:41:28 +08:00
    python

    bool(b) ^ bool(a)
    bool(b) != bool(a)
    misaka20038numbe
        7
    misaka20038numbe  
       2017-05-22 12:54:29 +08:00
    if((a == null or b == null) and a != b)
    weyou
        8
    weyou  
       2017-05-22 18:58:07 +08:00 via Android
    python
    bool(a) is not bool(b)
    SoloCompany
        9
    SoloCompany  
       2017-05-22 20:59:22 +08:00
    @esolve java 一样有 ^ 操作符啊
    pagxir
        10
    pagxir  
       2017-05-22 22:04:54 +08:00
    if((a==null) != (b==null))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4131 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:29 · PVG 13:29 · LAX 22:29 · JFK 01:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.