I am working on a script that asks for an input to be given between 1 and 100. I would like to have the restriction that the input is limited to between 1 and 100, and that the number must be a whole number.Here is the code:
number=input('Enter a whole number between 1 and 100:')
while number<1 || number>100
display('Not a valid number please try again')
number=input('Enter a whole number between 1 and 100:')
end
I can't figure out how to get it to set a restriction to non-whole number values. Please let me know how to specify this. I have been searching for a while and haven't found the solution.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 1월 29일

0 개 추천

Suprisingly, just run your specification and it works in MATLAB
number=input('Enter a whole number between 1 and 100:')
while number<1 || number>100
display('Not a valid number please try again')
number=input('Enter a whole number between 1 and 100:')
end

댓글 수: 2

John Furman
John Furman 2020년 1월 29일
I would like to restrict the input to where if you put in a decimal value, ex. 4.5, the script will display Not a valid number please try again.
change to
while number<1 || number>100 || round(number)~=number

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2020년 1월 29일

0 개 추천

validateattributes( number, { 'numeric' }, { 'scalar', '>=', 1, '<=', 100, 'integer' } )
You would have to put a message together yourself though if you don't want the error message itself returned on command line

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

제품

질문:

2020년 1월 29일

댓글:

2020년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by