Azzera filtri
Azzera filtri

Changing switch statement to while loop?

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Thorsten
Thorsten il 23 Ott 2015
Modificato: Thorsten il 23 Ott 2015
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')

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by