概述
while循环
%while循环
sum = 0; i = 1;
x = input('Enter first value:');
while x>=0
sum = sum+x;
x = input('Enter next value:');
i = i+1;
end
mean = sum/i;
fprintf('mean=%fn',mean);
for循环
%for循环
%计算一中的第几天
day_of_month = 0;
disp('spcified date');
year = input('Enter year:');
month = input('Enter month:');
day = input('Enter day:');
%判断是否是闰年
%函数mod判断一个数是否可以被另一个数整除
if mod(year,400)==0
lead_year = 1;
elseif mod(year,100)
lead_year = 0;
elseif mod(year,4)
lead_year = 1;
else
lead_year = 0;
end
for i = 1:month-1
switch(i)
case{1,3,5,7,8,10,12}
day_of_month = day_of_month + 31;
case{2}
if lead_year==0
day_of_month = day_of_month + 28;
else
day_of_month = day_of_month + 29;
end
case{4,6,9,11}
day_of_month = day_of_month + 30;
end
end
fprintf('day_num=%dn',day_of_month+day);
判断闰年
%
day_of_month = 0;
disp('spcified date');
year = input('Enter year:');
month = input('Enter month:');
day = input('Enter day:');
%判断是否是闰年
%函数mod判断一个数是否可以被另一个数整除
if mod(year,400)==0
lead_year = 1;
elseif mod(year,100)
lead_year = 0;
elseif mod(year,4)
lead_year = 1;
else
lead_year = 0;
end
for i = 1:month-1
switch(i)
case{3,5,7,8,10,12}
day_of_month = day_of_month + 31;
case{2}
if lead_year==0
day_of_month = day_of_month + 28;
else
day_of_month = day_of_month + 29;
end
case{4,6,9,11}
day_of_month = day_of_month + 30;
case{13}
break;
case{1}
continue;
end
end
fprintf('day_num=%dn',day_of_month+day);
最后
以上就是野性烤鸡为你收集整理的MATLAB程序设计-循环结构的全部内容,希望文章能够帮你解决MATLAB程序设计-循环结构所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复