필터 지우기
필터 지우기

how can I keep the user to input a value that is devisable by 7

조회 수: 2 (최근 30일)
mat geek
mat geek 2019년 3월 31일
댓글: madhan ravi 2019년 3월 31일
hello,
I think the answer to this is very simple. the code that I am trying to do is to keep asking for a number that is divisable by 7. once the user enters a number that is div by 7, the code ends. I got the part where the user input is div by 7 and ends, but I cant find a way to keep asking for input if the number is not div by 7. here is what I got.
usernum = input('enter a posative number: ');
i = 1;
deviby7 = rem(usernum, 7);
while i > 0
if deviby7 ~= 0
fprintf('incorrect input\n')
elseif deviby7 == 0
fprintf('correct input\n')
i = i -1;
end
end

답변 (1개)

madhan ravi
madhan ravi 2019년 3월 31일
while 1
usernum = input('enter a positive number: ');
deviby7 = rem(usernum, 7);
if ~deviby7
fprintf('correct input\n')
break
else
fprintf('incorrect input\n')
end
end
  댓글 수: 3
madhan ravi
madhan ravi 2019년 3월 31일
Very true, thank you Stephen.
madhan ravi
madhan ravi 2019년 3월 31일
If you had run the code you won’t be asking this question.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by