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일

0 개 추천

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일

1 개 추천

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.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2021년 1월 11일

댓글:

2021년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by