Validating each value using a while loop

조회 수: 2 (최근 30일)
Michael Doherty
Michael Doherty 2020년 4월 22일
댓글: Michael Doherty 2020년 4월 23일
I am writing a script that would validate a statement. The tasl is to promt the user for the values specified below. For each value entered by the user, use a while loop to validate each value according to below. The statement I am trying to validate is "Number of Subintervals" and the value has to be greater than 1. N>1 where N=subintervals.
This is what I got and it does not work.
N=input('enter a number of subintervals greather than one')
while N>1
if N<1
disp('Please enter a number greater than 1')
end
end
If there is anyway you could help that would be great as I am new to coding.
  댓글 수: 2
per isakson
per isakson 2020년 4월 23일
What should happen when
  • user enters 0
  • user enters 2
???
If the user enters 2 the while-loop of your code will loop forever.
Michael Doherty
Michael Doherty 2020년 4월 23일
If the user enters a number less than one, they should be prompted with a statement to enter a number greater than one. If the user enters a number greater than one, it should leave the loop to continue on to another statement validation.

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

답변 (1개)

BobH
BobH 2020년 4월 23일
I think your while loop needs to have the input() within it
While the user hasn't given a valid number
Ask for a number
if the number is good, leave the loop
else offer a suggestion
end while

카테고리

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