필터 지우기
필터 지우기

How do I make a while loop that will return to a statement unless a condition is met

조회 수: 1 (최근 30일)
I am trying to make a while loop that will goes on forever unless a condition is met, in this case I want the loop to keep repeating unless I enter the word imperial or metric. And when I enter other words it will continue ask the question "Please make a choice", the code i have here just opts out whenever i enter other words
clear,clc
x = [];
while isempty(x)
x = input('Please make a choice (imperial or metric) - ','s');
if strcmp(x,'imperial')
disp('yes')
if strcmp(x,'metric')
disp('no')
end
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 16일
If strcmp()
...
elseif strcmp()
...
else
Give message
x = ''
end
  댓글 수: 2
Quan Seah
Quan Seah 2018년 4월 16일
I tried your answer it seems like i still get the same problem when i enter words other than imperial and metric, it just opts out of the loop

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by