a program that allows the user to enter his or her year in school

3 次查看(过去 30 天)
Create a program that prompts the user to enter his or her year in school freshman,sophomore,junior, or senior.
The input will be a string. Use the switch/case structure to determine which day finals will be given for each group Monday for freshman, Tuesday for sophomores, Wednesday for juniors, and Thursday for seniors. Handle the error case when the input is incorrect
this is part of the code that i have done for now:
final=input('enter your year in school:'s');
switch(final)
case{'freshman'},
final_day=monday
case{'sophomores'},
final_day=Tuesda
case{'juniors'},
final_day=wednesday
case{'seniors'},
final_day=thursday
otherwise,
disp('no a valid input');
final_day=0
end
fprintf('the final will be given %d\n',final_day)
is this right? any suggestion for my code would be appreciated

回答(2 个)

Walter Roberson
Walter Roberson 2013-4-2
No. You have not initiaized the variables "monday', "Tuesda", "wednesday" or "thursday".
Also, 'enter your year in school:'s' is not a valid string. One single-quote ends a string, but you need whitespace or punctuation or an operation after a string and before the next (undefined) variable "s" whose complex conjugate you are taking.
s'
means the complex conjugate of the variable "s".
If you want an apostrophe to appear in the prompt string then you need to code two of them in a row,
final=input('enter your year in school:''s');

Image Analyst
Image Analyst 2013-4-2
Close. Nice try. I've added some corrections with comments:
studentsYear = input('Enter your year in school: ', 's');
% Renamed from final to studentsYear to be more descriptive.
% Fixed the messed up s at the end.
switch(lower(studentsYear)) % Switch to lower case so we do not have to worry about case.
case 'freshman' % No comma or braces necessary
final_day = 'Monday'; % Make it a string.
case 'sophomore'
final_day = 'Tuesday'; % Added y to Tuesda
case 'junior'
final_day = 'Wednesday'; % final_day, not final_da
case 'senior'
final_day = 'Thursday';
otherwise % Using fprintf rather than disp.
fprintf('Not a valid input.\nYou must enter freshman, sophomore, junior, or senior.\n');
final_day=0; % Semi colon to suppress echo.
end
if ischar(final_day)
% Print to command window only for a valid entry.
fprintf('Your final will be given on %s.\n', final_day); % %s not %d since final_day is a string.
end

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by