How can do this?
1 次查看(过去 30 天)
显示 更早的评论
I need to do an exercise where i ask the USER for positive numbers, and while the number is positive i need to keep asking the USER for positive numbers. Then i need to do a sum of those positive numbers e show how many positive numbers the USER typed. This is my code thus far:
numero=input('Enter positive number')
while numero>=0
x=input('Enter another positive number')
end
w=[numero,x];
u=numel(w);
v=sum(w);
break
end
fprintf('Total positive numbers is %f\n',u)
fprintf('The sum is %f\n',v)
The problem is i can only enter 2 numbers and then it gives me the asnwer right away .HELP
0 个评论
采纳的回答
更多回答(1 个)
David Hill
2021-1-22
c=1;
while 1
n(c)=input('Enter positive number');
if n(c)<0
n=n(1:end-1);
break;
end
c=c+1;
end
fprintf('Total positive numbers is %f\n',numel(n));
fprintf('The sum is %f\n',sum(n));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!