How to use for loop
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Point Cloud Processing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!