OR运算符的问题

时间:2015-01-31 19:05:32

标签: c# windows-phone-8 windows-phone

我正在使用foreach循环,它根据列表中的项目数量重复自己。每次重复时,它都会检查两个文本框的值是否为“0”。如果他们(或其中一个)做了,那么我检查它正在迭代的小时的汇总值是(或者不是。在“结束”评论之后的部分的情况下)某个值。如果不是,那么我使用OR运算符(||)对下一个运算符做同样的事情。但是,这不起作用。

我正在使用debug.writeline查看循环找到匹配的位置。无论出于何种原因,它始终为0 - 即使迭代0的hour.summary值为“Rain”,也应该使得if无法找到匹配而是激活else。

foreach (var hour in result.Hourly.Hours)
{
    //Start
    if (starttemp.Text == "0")
    {
        if ((hour.Summary == "Rain") || (hour.Summary == "Snow") || (hour.Summary == "Drizzle") || (hour.Summary == "Light Rain") || (hour.Summary == "Light Snow") || (hour.Summary == "Heavy Rain") || (hour.Summary == "Heavy Snow"))
        { starttemp.Text = hour.Temperature.ToString(); Debug.WriteLine("SET START WITH " + hour.Summary + " AT HOUR " + increment); }
        else
        { }
    }
    //End
    if (endtemp.Text == "0")
    {
        if ((hour.Summary != "Rain") || (hour.Summary != "Snow") || (hour.Summary != "Drizzle") || (hour.Summary != "Light Rain") || (hour.Summary != "Light Snow") || (hour.Summary != "Heavy Rain") || (hour.Summary != "Heavy Snow"))
        { endtemp.Text = hour.Temperature.ToString(); Debug.WriteLine("SET END WITH " + hour.Summary + " AT HOUR " + increment); }
        else
        { }
    }
    increment = increment + 1;
}

1 个答案:

答案 0 :(得分:2)

该行

if ((hour.Summary != "Rain") || (hour.Summary != "Snow") || (hour.Summary != "Drizzle") || (hour.Summary != "Light Rain") || (hour.Summary != "Light Snow") || (hour.Summary != "Heavy Rain") || (hour.Summary != "Heavy Snow"))

永远是真的......

我不是100%确定你要做什么,但我认为你应该替换||与&&