I am working on homework. I am to prompt user for temperature in Celsius and then ask them to choose if they want it converted to F or C. I have this so far. It promted user for temp in C and converted to F before I started to add a while loop,
조회 수: 28 (최근 30일)
이전 댓글 표시
I am working on homework. I am to prompt user for temperature in Celsius and then ask them to choose if they want it converted to Farenheit or Kelvin. I have this so far. It promted user for temp in C and converted to F before I started to add a while loop, which is how I was thinking to get the user to choose the input, I also made the display a comment while I play with it. My question really is how to write ask user to choose Farenheit or Kelvin for the answer. I think maybe ifelse would work better. Also I did try to get with my professor about this he is not available before this is due. Just need some help.
Thanks yall
%This will take temp in Celsius and return it in either Farenhiet or Kelvin
C = input('Please enter a temperature value in Celsius.');
K = C+273.15
F = (9/5)*C+32
while 1
end
%fprintf('The temperature in Farenheit is: %d.\n', F)
댓글 수: 1
James Tursa
2019년 11월 5일
You get my vote for stating up front that this is homework and showing some effort.
채택된 답변
Jess Lovering
2019년 11월 5일
편집: Jess Lovering
2019년 11월 5일
You could use the question dialogue box, see the example below. You could first prompt for the temperature number and then ask if they want Farenhiet or Kelvin with the questdlg function similar to the example below. Instead of the "desert =" lines you would substitue in your code for the right conversion formula. You will only need 2 cases for your assignment. Hope this helps get your started in the right direction.
answer = questdlg('Would you like a dessert?', ...
'Dessert Menu', ...
'Ice cream','Cake','No thank you','No thank you');
% Handle response
switch answer
case 'Ice cream'
disp([answer ' coming right up.'])
dessert = 1;
case 'Cake'
disp([answer ' coming right up.'])
dessert = 2;
case 'No thank you'
disp('I''ll bring you your check.')
dessert = 0;
end
댓글 수: 4
Jess Lovering
2019년 11월 7일
Looks great! I have gotten so much use out of MATLAB answers that other people have asked so I have started to try to answer some myself when I get the chance. I tested it out and it worked well for me. It does print a lot to the screen when you run it, so you can suppress that with a semicolon at the end of those lines if you want it to only display the lines based on the user selection:
C = input('Please enter temperature in Celsius');
KorF = input('Would you like to convert to K or F?','s');
F = (9/5)*C+32;
K = C+273.15;
if KorF == 'K'
K = C+273.15; %This converts Kelvin to Celsius
disp(['The temperature in Kelvin is: ',num2str(K),'.'])
elseif KorF == 'F'
F = (9/5)*C+32;
disp(['The temperature in Farenheit is: ',num2str(F),' '.' ])
end
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!