My code keeps breaking for a few data input, Please help! The Question is: Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise fa

1 次查看(过去 30 天)
function valid = valid_date(year, month, day)
valid = false;
if isscalar(year) && year>0 && isscalar(month) && month~=0 && isscalar(day) && day>0
if mod(year,400)==0 || (mod(year,4) == 0 && mod(year,100) ~= 0)
if month==2
if day <= 29
valid = true;
end
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =true;
fprintf('good\n')
end
elseif (month==1||month==3||month==5||month==7||month==9||month==11)
if day<=31
valid = true;
end
else
valid = false;
end
else
if month==2
if day <= 28
valid = true;
end
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =false;
end
elseif (month==1||month==3||month==5||month==7||month==9||month==11)
if day<=31
valid = true;
end
else
valid = false;
end
end
end
end
  1 个评论
Faris Shaikh
Faris Shaikh 2019-6-17
The last day of every month
Variable valid has an incorrect value. valid_date(2014, 8, 31) failed...
Random dates
Variable valid has an incorrect value. valid_date(1399, 8, 31) failed...

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2019-6-17
编辑:Image Analyst 2019-6-17
Step through it with the debugger, like we all do. If you don't know how, see this link
So when month is 8, and day is 31, what do you think this will return:
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =false;
end
Do you think it will return true or false for "valid"? All those months should have valid true if (day <= 31) && (day >= 1), not day<=30. You don't even need the next if.
elseif (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
if (day >= 1) && (day <= 31)
valid = true;
else
valid =false;
end
Also, I would not name variables year, month, or day since those are built-in functions.
  3 个评论
Image Analyst
Image Analyst 2019-6-17
Well, I know it does. But I thought that seeing my code would make you realize that you had completely wrong days for many of the months and then you, being a bright engineer, would correct it. Sounds like homework and I'm not sure how much of your homework you're allowed to let others do for your. If you still can't figure it out let me know.
I've gotten ill-advised suggestions from people before but nobody has ever told me to sue them over it ?. Sounds a bit harsh. Maybe just an apology from them would be fine and you could avoid the litigation. ?
Faris Shaikh
Faris Shaikh 2019-6-17
Thank you for your resposnse, it has made me realize how at fault I am and how I need to correct me behaviour. I appreciate that you are still willing to help. I am not a computer science engineer, actuallly studying aerospace instead, and find computer programming difficult. I am better at other classes. Please forgive me for my inappropriate behaviour and thanks for the tips.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by