Need help with this question I was asked to preform

1 visualización (últimos 30 días)
Lucas Kimmel
Lucas Kimmel el 19 de Feb. de 2016
Editada: MHN el 19 de Feb. de 2016
Create a script file that will prompt the user for a mass in kg, and then prompt the user again if they would like to convert the mass to ‘lbm’ (for pounds mass) or ‘slugs’ (for slugs). The script should contain a switch statement with two cases and fprintf to the command window the corresponding mass in the units specified by the user. The format of your mass output should be 3 places after the decimal point. Also, include an error statement if neither case is selected (hint: use otherwise). You can type “help error” in the command window to understand how to create an error statement. For example, your command window might contain these input and output statements: Enter the mass in kilograms (kg): 10 Do you want to convert to ‘lbm’ or ‘slugs?: slugs The mass is 0.685 slugs.
  3 comentarios
Lucas Kimmel
Lucas Kimmel el 19 de Feb. de 2016
Yes I understand that. I guess I should of been more specific with what exactly I need help with. I am having trouble with the switch statement and I'm not exactly sure how it works
jgg
jgg el 19 de Feb. de 2016
Editada: jgg el 19 de Feb. de 2016
But basically, you give it a switching variable, then provide a series of cases. In your example, you'd want to switch on the input string that the user provided (call it units). Then, your switch statement would look like this:
switch units
case 'lbs'
%do stuff
disp('chose lbs')
case 'slugs'
%do stuff
disp('chose slugs')
otherwise
%throw an erro
disp('chose error')
end
You still need to choose what to do in each case, and handle the input and errors properly.

Iniciar sesión para comentar.

Respuestas (1)

MHN
MHN el 19 de Feb. de 2016
Editada: MHN el 19 de Feb. de 2016
We are not going to give you the answer, since this is a basic question which is trying to teach you not only how should you code in Matlab but also how should you learn a programming language! Instead, let me help you with the bigger picture, which is how should you solve such questions! Go to Google, and search "switch matlab", then the top one is: http://www.mathworks.com/help/matlab/ref/switch.html Read the documentation and the examples and you would definitely learn how "switch" works. Otherwise, you would never learn it if we provide the answer.

Categorías

Más información sobre Install Products 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