Info

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

Whats wrong with my code,I'm not able to get though my test if the month is 12?

1 次查看(过去 30 天)
4. Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the difference between the ages of the two children in days. Make sure to check that the input values are of the correct types and they represent valid dates. If they are erroneous, return -1. An example call to the function would be >> dd = day_diff(1,30,2,1); 2 which would make dd equal 2. You are not allowed to use the built-in function datenum or datetime.
function a = day_diff(month1,day1,month2,day2)
if ((month1 && month2)==(1 || 3 || 5 || 7 || 8 || 10 || 12))
month1 = month1*31;
month2=month2*31;
else if ((month1 && month2)==(4 || 6 || 9 || 11))
month1 = month1*30;
month2=month2*30;
else
month1=28;
month2=28;
end
end
finday1=month1+day1;
finday2=month2+day2;
a=abs(finday1-finday2);
end
  5 个评论
Geoff Hayes
Geoff Hayes 2018-2-11
also look at
month1 = month1*31;
month2 = month2*31;
If month1 is 1 (January) and month2 is 12 (December), what is the intent of the above code? It is almost as if you are assuming that every month has 31 days (12*31) and then the
a=abs(finday1-finday2);
would make sense. But that assumption is invalid...

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by