필터 지우기
필터 지우기

Changing switch statement to while loop?

조회 수: 2 (최근 30일)
Tyler Silva
Tyler Silva 2015년 10월 23일
편집: Thorsten 2015년 10월 23일
I'll paste my script, but I was wondering how to change my switch statement to a while loop. With the switch if the user enters incorrect info, they can only correct one piece of it. If i make a while loop then I want the user to be able to correct as much as they want. Do i need a while loop for each piece of info?

채택된 답변

Thorsten
Thorsten 2015년 10월 23일
편집: Thorsten 2015년 10월 23일
You can use the menu in the while loop as follows:
while menu('Information Correct?','Yes','No') == 2
wrong = menu('What is Wrong?','Name','Age','City','State','Zip');
switch wrong
case 1 % Name is Wrong
Name = input('Enter your name: ','s');
case 2 % Age is Wrong
Age = input('Enter your age: ');
case 3 % City is Wrong
City = input('Enter your City: ','s');
case 4 % State is Wrong
State = input('Enter your State: ','s');
case 5 % Zip Wrong
Zip = input('Enter you Zip code: ');
end
end
% while loop left, everything is correct
fprintf('Nice Work! \n')

추가 답변 (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