试图实现多个条件运算符

时间:2016-12-05 07:29:00

标签: c#

我试图实现多个条件运算符,但问题是,如果条件为真则总是如此。

我的要求是

如果Headername[CI]== "Yellow"

objnamewritten[CI]== "Banana" or objnamewritten[CI] == "Sun" or objnamewritten[CI] == "lemomn"

然后弹出窗口(ppup)将会打开,如果还有其他objnamewritten[CI],则弹出窗口(ppup1)将会打开。

private void common()
{
    if (Headername[CI]== "Yellow")
    {
        if ((objnamewritten[CI]== "Banana" | objnamewritten[CI] == "Sun")|(objnamewritten[CI] == "lemomn" | objnamewritten[CI] == "Sun"))
        {
            ppup.Height = Window.Current.Bounds.Height;
            ppup.IsOpen = true;
        }
        else
        {
            ppup1.Height = Window.Current.Bounds.Height;
            ppup1.IsOpen = true;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您的代码中缺少两件事。

  1. 您没有正确使用OR运算符
  2. 您应该使用Equals方法来比较字符串(一个好的做法)
  3. 以下代码应该有效:

        private void common()
        {
             if (Headername[CI] == "Yellow")
             {
                     if (objnamewritten[CI].Equals("Banana") || objnamewritten[CI].Equals("lemomn") || objnamewritten[CI].Equals("Sun"))
                    {
                         ppup.Height = Window.Current.Bounds.Height;
                         ppup.IsOpen = true;
                    }
                    else
                    {
                       ppup1.Height = Window.Current.Bounds.Height;
                       ppup1.IsOpen = true;
                    }              
              }
              else
              {
                       ppup1.Height = Window.Current.Bounds.Height;
                       ppup1.IsOpen = true;
              }
       }