Problem with a loop

2 visualizaciones (últimos 30 días)
Lucas Tapia Sánchez
Lucas Tapia Sánchez el 25 de Mzo. de 2022
Comentada: Voss el 25 de Mzo. de 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

Respuesta aceptada

Voss
Voss el 25 de Mzo. de 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 comentarios
Lucas Tapia Sánchez
Lucas Tapia Sánchez el 25 de Mzo. de 2022
Wow that was fast, thank you :)
Voss
Voss el 25 de Mzo. de 2022
You're welcome!
Be sure to test it and make sure it does what you're trying to do!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by