Problem with a loop
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!