为什么这不写“文字框”中的“周末”或“甚至周末”?

时间:2013-11-10 19:47:38

标签: c# string if-statement int

我试着用文字框写“偶尔周末”或“周末”,但没有出现。

DateTime thisDate1 = DateTime.Now;
int day = thisDate1.DayOfYear;
int week = day / 7;
bool even = true;
if ((week % 2) == 0) even = true;
if ((week % 2) == 1) even = false;

if (even == true)
{
    if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Even Weekend";
    if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Even Weekend";
}
if (even == false)
{
    if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Weekend";
    if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Weekend";
}

非常感谢您的每一个答案。

2 个答案:

答案 0 :(得分:4)

我假设你今天在星期天测试它。但DayOfWeek.Sunday0,而不是7

答案是转换为int:

//if ((int)thisDate1.DayOfWeek == 6) textBox1.Text = "Even Weekend";
//if ((int)thisDate1.DayOfWeek == 7) textBox1.Text = "Even Weekend";
  if (thisDate1.DayOfWeek == DayofWeek.Saturday)  textBox1.Text = "Even Weekend";
  if (thisDate1.DayOfWeek == DayofWeek.Sunday)    textBox1.Text = "Even Weekend";

答案 1 :(得分:0)

为什么在提供DayOfWeek枚举类型时使用Ints来检查日期 http://msdn.microsoft.com/en-us/library/system.dayofweek%28v=vs.110%29.aspx

if (even == true)
            {
                if (thisDate1.DayOfWeek == DayOfWeek.Sunday) textBox1.Text = "Even Weekend";
                if (thisDate1.DayOfWeek == DayOfWeek.Saturday) textBox1.Text = "Even Weekend";


        }
    if (even == false)
    {
        if (thisDate1.DayOfWeek == DayOfWeek.Sunday) textBox1.Text = "Weekend";
        if (thisDate1.DayOfWeek == DayOfWeek.Saturday) textBox1.Text = "Weekend";

    }

为什么要使用数字?