Dealing with wrong input
조회 수: 42 (최근 30일)
이전 댓글 표시
How do I avoid this error? I would like for users, if they enter a string instead of number to told "Those are not the instructions, please try again"
댓글 수: 4
답변 (2개)
Nicolas B.
2019년 9월 3일
편집: Nicolas B.
2019년 9월 3일
Okay, then I would use the exception catching method:
str = '';
num = NaN;
while isnan(num)
try
num = input('Please enter your guess: ');
catch
disp('You must give a valid number!');
num = NaN;
end
end
댓글 수: 1
Rik
2019년 9월 3일
That's also possible, although I think it would make slightly more sense to use Stephen's method, because then it also isn't possible to provide a variable name.
If you define a=9; before your loop, you can enter a in the prompt and it will pass the test. With Stephen's code, that is not possible.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!