Info

此问题已关闭。 请重新打开它进行编辑或回答。

What is wrong with my function? (Lesson 5 wrap up assignment)

2 次查看(过去 30 天)
function valid = valid_date (year,month,day)
if nargin <3
valid = false;
return
end
if (isscalar(year)) && (year >= 1) && (year == fix(year))
valid = true;
elseif (isscalar(month)) && (month >= 1) && (month==fix(month))
valid = true;
elseif (isscalar(day)) && (day >= 1) && (day == fix(day))
valid = true;
else
valid = false;
end
if ((rem(year,4) == 0) || (rem(year,400)==0)) && (rem(year,100)~=0)
if (month == 2) && (day<30)
valid = true;
elseif (month == 1||3||5||7||9||11) && (day<32)
valid = true;
elseif (month == 4||6||8||10||12) && (day<31)
valid = true;
else
valid = false;
end
else
valid = false;
return
end
if (month == 1||3||5||7||9||11) && (day<32)
valid = true;
elseif (month ==2) && (day<29)
valid = true;
elseif (month == 4||6||8||10||12) && (day<31)
valid = true;
else
valid = false;
end
end

回答(2 个)

IMAD ACHAHBOUN
IMAD ACHAHBOUN 2021-4-20
function [valid] = valid_date(year,month,day)
if nargin==3
if month==fix(month) && isscalar(month) && day==fix(day) && isscalar(day) && year==fix(year) && isscalar(year) && isscalar(valid_date) && year>0 && month>0 && day>0
if mod(year,4)==0&&mod(year,100)~=0 || mod(year,400)==0&&mod(year,100)==0
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif month==2 && ismember(day,[1:29])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
else
valid=false;
end
else
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
elseif month==2 && ismember(day,[1:28])
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end

Abhishek Inamdar
Abhishek Inamdar 2021-6-13
function valid = valid_date (year, month, day)
if nargin == 3
if isscalar(month) && isscalar(day) && isscalar(year) && month==fix(month) && day==fix(day) && year==fix(year) && year>0 && month>0 && day>0
if mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif month==2 && ismember(day,[1:29])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
else
valid=false;
end
else
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
elseif month==2 && ismember(day,[1:28])
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end
Keep isscalar first in the line 3 is importatnt, first we need to check scalar

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by