Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
marie el 2 de Abr. de 2013
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');
disp('no a valid input');
fprintf('the final will be given %d\n',final_day)
is this right? any suggestion for my code would be appreciated

Respuestas (2)

Walter Roberson
Walter Roberson el 2 de Abr. de 2013
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.
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 el 2 de Abr. de 2013
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.
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.


Más información sobre Characters and Strings en Help Center y File Exchange.



Community Treasure Hunt

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

Start Hunting!

Translated by