필터 지우기
필터 지우기

About While Loops problems

조회 수: 60 (최근 30일)
CHUN HIN KYLE
CHUN HIN KYLE 2024년 5월 19일 6:03
댓글: VBBV 2024년 5월 20일 22:31
Is there anyway to end the program by entering "quit"? As the program keeps going despite displays "Exiting..."
The following is the syntax.
function Individual_Project_Final()
disp('Welcome to Unit Converter interface! ');
disp(' ');
while true
% Accept user input
input_str = input('Enter a number to be converted (or type "quit" to exit program): ', 's');
% Check if the user wants to quit
if strcmp(input_str, 'quit')
disp('Exiting...')
break;
end
% Convert input string to a number
numin = str2double(input_str);
% Check if the input is a valid number
if isnan(numin)
disp('Input invalid. Please enter a whole or decimal number (or type "quit to exit program): ');
else
disp('Input valid. Now please select the unit conversion type (or type "quit to exit program): ');
break;
end
end
% Display the menu for selecting conversion type
disp(' ')
disp('Select conversion type:')
disp('1) Celsius to Fahrenheit')
disp('2) Fahrenheit to Celsius')
disp('3) Centimeters to Inches')
disp('4) Inches to Centimeters')
disp('5) Meters to Foot')
disp('6) Foot to Meters')
disp('7) Kilometers to Miles')
disp('8) Miles to Kilometers')
disp('9) Grams to Ounces')
disp('10) Ounces to Grams')
disp('11) Kilograms to Pounds')
disp('12) Pounds to Kilograms')
disp('13) Tonnes to Tons')
disp('14) Tons to Tonnes')
while true
% Accept user input
input_str = input('Please select conversion type (1-14) (or type "quit" to exit): ', 's');
% Check if the user wants to quit
if strcmp(input_str, 'quit')
disp('Exiting...');
break;
end
% Convert input string to a number
convtype = str2double(input_str);
% Check if input is a valid conversion type
if ~ismember(convtype, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
disp('Conversion type invalid. Please enter a number from 1 to 14 (or type "quit to exit program): ');
else
disp('Input valid. We are now finalizing the resluts based on your input. ');
break;
end
end
% Call the user-defined function from Module 2
[numout,unit] = MyUnitConverter(numin,convtype);
% Output generation
disp(' ');
fprintf('The converted number is: %.2f %s\n', numout, unit);
end
  댓글 수: 3
Stephen23
Stephen23 2024년 5월 20일 8:14
편집: Stephen23 2024년 5월 20일 8:15
I suspect that you want RETURN rather than QUIT or BREAK.
VBBV
VBBV 2024년 5월 20일 11:00
@Stephen23 yes, return would be more suitable than quit or break

댓글을 달려면 로그인하십시오.

답변 (1개)

VBBV
VBBV 2024년 5월 19일 6:26
It has already exited, the first while loop, but it's waiting for next input from user ... See the message at bottom left of window in workspace, you need to select or provide a number
  댓글 수: 9
CHUN HIN KYLE
CHUN HIN KYLE 2024년 5월 20일 14:52
Sorry, the loop just repeated itself instead of moving down after replacing "break" with "continue" after else part of code when the user enters inputs that are valid.
VBBV
VBBV 2024년 5월 20일 22:31
Try using the code which i modified (that has only one while loop , inner while loop is commented )

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by