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,

조회 수: 38 (최근 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)

채택된 답변

Jess Lovering
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
Adam Juckniewitz
Adam Juckniewitz 2019년 11월 6일
Just wanted to say thanks agian! Here's how it turned out.
It's probably cluttered, I put the variables in twice to be sure. But still in progress. Thankful for this community.
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
Jess Lovering
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by