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

Respuestas (1)

Stephen23
Stephen23 el 19 de Sept. de 2016
Editada: Stephen23 el 19 de Sept. de 2016
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

Categorías

Preguntada:

el 19 de Sept. de 2016

Editada:

el 19 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by