Help on while loop to make user enter an accepted value

조회 수: 1 (최근 30일)
Aaron LaBrash
Aaron LaBrash 2022년 4월 24일
편집: DGM 2022년 4월 24일
I need to have the user enter a number between 0 and 100, and have a while loop that prompts them to re-enter the value if they input one that is not valid.
using this format I can only make it work in the reverse of what I need
pC = input('enter initial volume of water, in percent of total tank capacity:','s')
while ((pC<0)&(pC>100))
if (pC>=0)&(pC<=100)
pC
else input('enter initial volume of water, in percent of total tank capacity:','s')
end
end
any advice would be greatly appreciated

답변 (1개)

DGM
DGM 2022년 4월 24일
편집: DGM 2022년 4월 24일
pC can't be <0 && >100
This should fix the issue and simplify things
pC = -1; % initialize
while (~isnumeric(pC) || (pC<0) || (pC>100))
pC = input('enter initial volume of water, in percent of total tank capacity: ');
end
pC

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by