How to use for loop

조회 수: 1 (최근 30일)
Inifome Eleluwor
Inifome Eleluwor 2021년 1월 11일
댓글: Inifome Eleluwor 2021년 1월 11일
Hello, I'm supposed to make a code that prompts the user to enter a positive real number to 1 decimal place. I should prompt the user 10 times. This was the code I used.
user_input = fprintf('%.1f', input('Enter a positive real number'));
for i = 1:10
if user_input <= 0
user_input = fprintf('%.1f', input('Enter a positive real number'));
end
end

채택된 답변

WalterWhite
WalterWhite 2021년 1월 11일
user_input = fprintf('%.1f\n', input('Enter a positive real number\n'));
for i = 1:9 %first input outside the loop;so 9 more inputs
if user_input >= 0
user_input = fprintf('%.1f\n', input('Enter a positive real number\n'));
end
end
  댓글 수: 1
Inifome Eleluwor
Inifome Eleluwor 2021년 1월 11일
thank you for your answer. Very appreciated.

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

추가 답변 (1개)

KSSV
KSSV 2021년 1월 11일
user_input = fprintf('%.1f\n', input('Enter a positive real number'));
count = 0 ;
while count <= 10
if user_input >= 0
count = count+1 ;
user_input = fprintf('%.1f\n', input('Enter a positive real number'));
end
end
  댓글 수: 1
Inifome Eleluwor
Inifome Eleluwor 2021년 1월 11일
thank you for your answer. Very appreciated.

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

카테고리

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