简体   繁体   English

if语句中的&&运算符

[英]And & Or operators in if statements

I've made a date of birth program that display your DoB, it will also factor in leap years so that if you enter year as 1995, month 2, day 29, it will through an error because it wasn't a leap year. 我已经制定了一个显示你的DoB的出生日期计划,它也会考虑到闰年,所以如果你输入1995年,第2个月,第29天的年份,它将会出错,因为它不是闰年。 It also takes into a how many days each month has and this is that part I want help with. 它还需要每个月有多少天,这是我需要帮助的部分。 Currently, this is how it is: 目前,它是这样的:

           if (month == 1 / && day > 31)
            {
                Console.WriteLine("January only has 31 days - Enter again");
                validDay = false;
            }
            else if (day >= 29 && !IsLeapYear(year) && month == 2)
            {
                Console.WriteLine("You were not born on a leap year or February only has 28 days - Enter again");
                validDay = false;
            }
           //And so on for each month

So I then tried it like this: 所以我接着尝试了这样:

if (month == 1 || month == 3 || month == 5 || month == 7 || 
                month == 8 || month == 10 || month == 12 && day > 31)
            {
                Console.WriteLine("Your birth month only has 31 days - Enter again");
                validDay = false;
            }
            else if (day >= 29 && !IsLeapYear(year) && month == 2)
            {
                Console.WriteLine("You were not born on a leap year or February only has 28 days - Enter again");
                validDay = false;
            }
            else if (month == 3 || month == 6 || month == 9 || month == 11 && day > 30)
            {
                Console.WriteLine("Your birth month only has 30 days - Enter again");
                validDay = false;
            }

And for some reason it will always throw the error message even if you enter 1997, 5, 28. The month chosen in 5 but the day is not > 31 so why does it do this? 并且由于某种原因,即使你输入1997,5,28,它也总会抛出错误信息。在5中选择的月份但是当天不是> 31,那为什么会这样做呢? I'm also open up to different ways to do this because I have a feeling there's another way - arrays maybe? 我也开放了不同的方法来做到这一点,因为我有一种感觉还有另一种方式 - 阵列可能吗?

Try adding some parenthesis 尝试添加一些括号

if ((month == 1 || month == 3 || month == 5 || month == 7 || 
month == 8 || month == 10 || month == 12) && day > 31)

Without parethesis the last && refers only to the last month. 没有parethesis,最后&&仅指上个月。

You can also use the DateTime constructor and see if it throws an exception ( http://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110).aspx ) or use DateTime.TryParse. 您还可以使用DateTime构造函数,看看它是否抛出异常( http://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110).aspx )或使用DateTime.TryParse。

我建议你使用DateTime.TryParse - 它会自动验证所有日期,然后你可以向用户返回类似“无效日期”的内容。

Operators have something called precedence order . 运算符具有称为优先顺序的东西。 Try using parentheses in order to achieve the precedence you wish to achieve. 尝试使用括号以实现您希望实现的优先级。

Why not use DateTime.TryParse. 为什么不使用DateTime.TryParse。

Please have a look at the following links as well: 请查看以下链接:

Validating Date (Leap year or not) before inserting into Database 在插入数据库之前验证日期(闰年与否)

Validate a DateTime in C# 在C#中验证DateTime

I would recommend using DateTime.TryParseExact as follows... 我建议使用DateTime.TryParseExact如下...

string year = "1997";
string month = "5";
string day = "28";
string dateText = string.Format("{0}/{1}/{3}", year, month, day);
DateTime date;

if (!DateTime.TryParseExact(dateText, "yyyy/MM/dd", null, DateTimeStyles.None, out date))
{
    Console.WriteLine("Date is invalid");
}

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM