MatLab---while循环

1.while condition

action

end

2.输入三个正数

n=3;
for i=1:n
  inputnum=input(['enter a positive number[#' num2str(i) ']:'])
  while inputnum<=0
    fprintf('You enter a %f \n',inputnum);
    inputnum=input(['Enter a positive number[#' num2str(i) ']:']);
  end
  fprintf('ok\n');
end

3.求负数的平均数

inputnum=input('Enter a negative number:');
% if inputnum<0
% sum=0;
% counter=0;
% while inputnum < 0
% inputnum=input('Enter a negative number:');
% sum=sum+inputnum;
% counter=counter+1;
% end
% fprintf('the average is %f\n',sum/counter);
%
% else
% fprintf('No negative number to average.');
% end

 

posted @ 2022-04-21 11:45  无敌小金刚  阅读(612)  评论(0)    收藏  举报