检测31天的月份 [英] Detect months with 31 days
问题描述
是否存在以下代码的类似形式:
Is there an analogous form of the following code:
if(month == 4,6,9,11)
{
do something;
}
或者必须是:
if(month == 4 || month == 6 etc...)
{
do something;
}
我想写一个如果
检查本月是否超过31天的声明。
I am trying to write an if
statement that checks if this month has more than 31 days.
编辑
我想真正的问题是我和我所教的一些东西,但每次我尝试使用sun网站关于java时它都会让我感到困惑。我的问题是,如果我从一个用户和一天获得一个月的时间,我将其放入MM / dd格式并对其进行评估,那么是否有更简单的方法来检查月份和日期是否有效以及在我检查之后有效我可以用我所拥有的格式打印MM / dd。如果无效打印一行显示无效的月份或日期。
I guess the real problem is I undersand some of what I am taught but every time I try to use the sun website about java it just confuses me. My question is if I get a month in from a user and a day and I put it into a MM/dd format and evaluate it then is there an easier way to check if the month and the day is valid and after I check it for being valid I can either print the MM/dd in the format that I have. If it is not valid Print a line that says Invalid month or day.
推荐答案
如果您使用的是C或Java,那么可以这样做:
If you're using C or Java, you can do this:
switch (month) {
case 4:
case 6:
case 9:
case 11:
do something;
break;
}
在某些语言中,你甚至可以写案例4 ,6,9,11:
。
其他可能性是创建一个数组[4,6,9,11],如果[4,6,9,11]中的月份做某事,某些函数式语言应允许类似的东西;
In some languages, you could even write case 4,6,9,11:
.
Other possibilities would be to create an array [4,6,9,11], some functional languages should allow something like if month in [4,6,9,11] do something;
正如Lior所说,这取决于语言。
As Lior said, it depends on the language.
编辑:按顺便说一句,你也可以这样做(只是为了好玩,坏代码,因为不可读):
By the way, you could also do this (just for fun, bad code because not readable):
if ((abs(month-5) == 1) || (abs(month-10) == 1)) do_something;
这篇关于检测31天的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!