Need help with this question I was asked to preform

1 次查看(过去 30 天)
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 个评论
Lucas Kimmel
Lucas Kimmel 2016-2-19
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 2016-2-19
编辑:jgg 2016-2-19
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.

请先登录,再进行评论。

回答(1 个)

MHN
MHN 2016-2-19
编辑:MHN 2016-2-19
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.

类别

Help CenterFile Exchange 中查找有关 Install Products 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by