if 判断语句 switch-case判断语句 循环语句

if判断语句

求一个数的完全平方根

num=input('Please enter a number: ');
if num < 0
    disp('Ok,we ''ll use the absolute value');
    num = abs(num);
end
fprintf('The sprt of %f is %f \n',num,sqrt(num));
>> sprt
Please enter a number: 9
The sprt of 9.000000 is 3.000000 

function AutoReply()
reply = input('Input character(Y/N)=?','S');
if reply == 'Y'
    disp('positive command received');
end
if reply =='N'
    disp('negative command received');
end

>> NY
Input character(Y/N)=?y
>> NY
Input character(Y/N)=?Y
positive command received
function AutoReply()
reply = input('Input character(Y/N)=?','S');
if reply == 'Y'|| reply == 'y'
    disp('positive command received');
end
if reply =='N'|| reply == 'n'
    disp('negative command received');
end
>> NY
Input character(Y/N)=?y
positive command received
radius = input('Please enter the radius:');
if radius<0
    error('Sorry:%f is not a valid radius \n',radius);
else
    fprintf('the area is %f\n',calcarea(radius));
end
>> test
Please enter the radius:1
the area is 3.141593
>> test
Please enter the radius:-1
错误使用 test (line 3)
Sorry:-1.000000 is not a valid radius

Q:编写一个函数来确定一个输入参数是标量、向量还是矩阵?
T:为此,可以使用size函数来查找输入参数的维数。如果行数和列数都等于1,则输入参数为标量。另一方面,如果只有一个维是1,则输入参数是一个向量(行或列向量)。如果两个维数都不是1,则输入参数是一个矩阵。这三个选项可以使用嵌套的if-else语句进行测试。在本例中,从函数中返回单词“标量”、“向量”或“矩阵”。
A:

function outtype = findargtype(inputarg);
[r,c] = size(inputarg);
if r == 1 && c==1
  outtype = ' scalar ';
elseif r == 1 || c == 1
  outtype = ' vector ';
else
  outtype = ' matrix';
end
>> findargtype(33)
ans =
    ' scalar '
>> findargtype(2:5)
ans =
    ' vector '
>> findargtype(zeros(2,3))
ans =
    ' matrix'

Q:根据输入参数修改函数的类型以返回“标量”、“行向量”、“列向量”或“矩阵”。
A:

function outtype = findargtype(inputarg);
[r,c] = size(inputarg);
if r == 1 && c==1
  outtype = ' scalar ';
elseif r == 1 
  outtype = ' row vector ';
elseif c == 1
  outtype = ' column vector';
else
  outtype = ' matrix ';
end
end

Q:以下函数接收一个整数测验分数,其范围应该是从0到10。然后函数根据以下方案返回相应的字母等级:9或10是a,8是B,7是C,6是D,低于它的东西都是“F”。由于这些可能性是相互排斥的,我们可以使用单独的独立识别语句来实现分级方案。但是,有一个if-else语句和多个elseif子句更有效。此外,如果测试分数无效,则该函数将返回值“X”。该函数假设该输入是一个整数。
A:

function letGrade = letterGrade(numGrade)
if numGrade == 9 || numGrade == 10
    letGrade = 'A';
elseif numGrade == 8
    letGrade = 'B';
elseif numGrade == 7
    letGrade = 'C';
elseif numGrade == 6
    letGrade = 'D';
elseif numGrade < 0|| numGrade >10
    letGrade = 'X';
else 
    letGrade = 'F';
end
end
function letGrade = letterGrade(numGrade)
if numGrade < 0|| numGrade >10
    letGrade = 'X';
else
  switch numGrade
    case {10,9}
      letGrade = 'A';
    case 8
      letGrade = 'B';
    case 7
      letGrade = 'C';
    case 6
      letGrade = 'D';
    otherwise
      letGrade = 'F';
 end
end
>> letterGrade(8)
ans =
    'B'

isletter

>> isletter('A')
ans =
  logical
   1

isempty

>> isempty(rand(0,1))
ans =
  logical
   1
>> isempty(rand())
ans =
  logical
   0

isa

>> num=11
num =
    11
>> isa(num,'int16')
ans =
  logical
   0
>> isa(num,'double')
ans =
  logical
   1

iskeyword

>> iskeyword('function')
ans =
  logical
   1
>> iskeyword('if')
ans =
  logical
   1
>> iskeyword('switch')
ans =
  logical
   1
>> iskeyword('end')
ans =
  logical
   1

循环语句

for i = 1:10;
fprintf('loop variable:i=%d\n',i);
end
loop variable:i=1
loop variable:i=2
loop variable:i=3
loop variable:i=4
loop variable:i=5
loop variable:i=6
loop variable:i=7
loop variable:i=8
loop variable:i=9
loop variable:i=10
for i = 4:2:8;
fprintf('loop variable:i=%d\n',i);
end
loop variable:i=4
loop variable:i=6
loop variable:i=8
for i = 1:5;
fprintf('*\n');
end
>> test
*
*
*
*
*
inputnum=NaN(10,1);
for i = 1:10
inputnum(i)=input('Enter a number: ');
end
>> test
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: 9
Enter a number: 0
>> inputnum

inputnum =

     1
     2
     3
     4
     5
     6
     7
     8
     9
     0
n=randi([3,10]);
runsum=0;
for i = 1:n
inputnum=input('Enter a number: ');
runsum=runsum+inputnum;
end
>> test
Enter a number: 13
Enter a number: -10
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: 9
Enter a number: 0
>> runsum

runsum =

 42
n=randi([3,10]);
runproduct=1;
for i = 1:n
inputnum=input('Enter a number: ');
runproduct=runproduct*inputnum;
end
>> runproduct
 runproduct =
  15240960
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值