Problem with a loop

2 vues (au cours des 30 derniers jours)
Lucas Tapia Sánchez
Lucas Tapia Sánchez le 25 Mar 2022
Commenté : Voss le 25 Mar 2022
we are doing a project and I have problems with the main loop of the system, basically I want the loop to repeat itself if the answer is not within the range of our variable and for the loop to continue and exit the loop if it is within the range, also if the answer is correct the program has to ask if you are ok with your answer. So the problem comes when I put the wrong answer (the value is over the range of the variable) it does ask if you are ok with your answer and it is not suposed to do that
I also have to say this is not an entire project, is just a little part of it
function progwtf
repeat = 'yes';
while strcmp(repeat,'yes')|| strcmp(repeat,'YES')
a = -1;
while a<1
a = input('which is your height? (meters)');
if a<1.00
disp('that is not posible')
elseif a>2.30
disp('that is not posible')
end
end
repeat = input('Do you want to change it? (yes/no)','s');
end
If someone is kind enought to answer me pls do

Réponse acceptée

Voss
Voss le 25 Mar 2022
repeat = 'yes';
while strcmpi(repeat,'yes') % use strcmpi for case-insensitive comparison
while true % keep looping until a break statement is encountered
a = input('what is your height? (meters)');
if a<1.00 || a>2.30
disp('that is not likely'); % note: people can be very tall or very short (e.g., young children are short, typically)
continue % ask for the height again
end
break % break (i.e., exit the inner while-loop) when a is in the valid range
end
repeat = input('Do you want to change it? (yes/no)','s');
end
  2 commentaires
Lucas Tapia Sánchez
Lucas Tapia Sánchez le 25 Mar 2022
Wow that was fast, thank you :)
Voss
Voss le 25 Mar 2022
You're welcome!
Be sure to test it and make sure it does what you're trying to do!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by