现金或信贷问题

Jos*_*h K 2 theory if-statement boolean operators

如果你去商店询问"现金或信用卡?" 他们可能会简单地说"是".当你提出OR陈述时,这并没有告诉你任何事情.if(cash || credit)

对于人类来说,他们可能会回答"两个"这个问题,或"只有{现金|信用}".有没有办法(或运算符)强制a语句返回语句的各个TRUE部分?例如:

boolean cash = true;
boolean credit = true;
boolean check = false;

if(cash || credit || check)
{
    // In here you would have an array with cash and credit in it because both of those are true
}
Run Code Online (Sandbox Code Playgroud)

我想指出,这不是我想解决的问题.这是我在想的事情,并想知道是否有可能.我想不出我会有的实际应用.

Mar*_*ers 10

在C#中,您可以使用带有Flags属性集的枚举执行与此非常相似的操作.

[Flags]
enum MethodOfPayment
{
    None = 0,
    Cash = 1,
    Credit = 2,
    Check = 4
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

void Run()
{
    MethodOfPayment m = MethodOfPayment.Cash | MethodOfPayment.Credit;
    if (m != MethodOfPayment.None)
    {
        // You can now test m to see which values are selected.

        // If you really want the values in an array, you can do this:
        MethodOfPayment[] selected = getSelectedValues(m).ToArray();
        // selected now contains { Cash, Credit }
    }
}

// Helper method to get the selected values from the enum.
IEnumerable<MethodOfPayment> getSelectedValues(MethodOfPayment m)
{
    foreach (MethodOfPayment x in Enum.GetValues(typeof(MethodOfPayment)))
    {
        if ((m & x) != MethodOfPayment.None)
            yield return x;
    }
}
Run Code Online (Sandbox Code Playgroud)


whe*_*ies 5

在Scala中,您可以使用匹配语句编写以下内容

def OneOrTheOther( _1:Boolean, _2:Boolean ) = {
    (_1, _2) match{
        case True, False => //do stuff
        case False, True => //do stuff
        case True, True =>  //do stuff
        case False, False =>//do stuff
     }
}
Run Code Online (Sandbox Code Playgroud)

我喜欢比赛表达.