How can I keep asking user for correct input until given?

조회 수: 13 (최근 30일)
Jenn Garrison
Jenn Garrison 2017년 4월 13일
답변: KSSV 2017년 4월 13일
I want a question to continue to be prompted until correct input is given. Prompt the user to input a number between 0 and 100 (inclusive) and keep prompting the user until he does so and then display the number. I am a little lost as to hopw to start it. I have this so far
num = int(raw_input('Enter an integer between 0 and 100 '))
if num < 0 && num > 100;
fprintf 'Invalid input. Try again '
else:
num >= 0 && num <= 100;
disp (num)

채택된 답변

KSSV
KSSV 2017년 4월 13일
prompt = 'Enter an integer between 0 and 100: ';
x = input(prompt) ;
while x <0 || x > 100
x = input(prompt) ;
end

추가 답변 (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