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 Center および File ExchangePoint Cloud Processing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by