using a switch structure to do a problem
显示 更早的评论
Using the switch-case construction, write code that takes a variable named Month containing a string and assigns it to the variable numMonth, which represents the calendar month number (i.e. April is the 4th month in the calendar; numMonth is 4). Also based on Month, assign to the variable numDays the number of days in that month (for this problem, assume February has 28 days). Your code should be able to work for all months in the calendar. If the variable Month is not included in the Gregorian calendar (i.e. the one you're probably used to using), then the output variables numMonth and numDays should be 'NaN' and the following warning should be displayed: "<string stored in Month> is not a month in the Gregorian calendar.". Note: Your code should be able to handle Month written in all cases, for example: 'July' and 'juLy' should have the same output.
I am confused....so I ask the user to input month like january ? please help
回答(1 个)
switch lower(Month)
case 'february'
numMonth = 2;
numDays = 28;
case 'april'
numMonth = 4;
numDays = 30;
otherwise
numMonth = NaN;
numDays = NaN;
fprintf('"%s is not a month in the Gregorian calendar."\n',Month)
end
类别
在 帮助中心 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!