Problem with a loop

2 次查看(过去 30 天)
Lucas Tapia Sánchez
评论: Voss 2022-3-25
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

采纳的回答

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

请先登录,再进行评论。

更多回答(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