Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
What is wrong with my function? (Lesson 5 wrap up assignment)
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
IMAD ACHAHBOUN
am 20 Apr. 2021
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
0 Kommentare
Abhishek Inamdar
am 13 Jun. 2021
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
0 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!