MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [求助]switch语句中的otherwise (https://www.labfans.com/bbs/showthread.php?t=2544)

tguranus 2008-04-23 17:54

[求助]switch语句中的otherwise
 
程序段如下:
function distance=metric(x,y)
if x==y
distance=0;
else
distance=1;
end
switch(y)
case 0
if x==0
distance=0.0458;
end
if x==1
distance=2;
end
if x==2;
distance=1.0458;
end
case 1
if x==0
distance=2;
end
if x==1
distance=0.0458;
end
if x==2
distance=1.0458;
end
otherwise
[COLOR="Red"]break[/COLOR];
end
这是一个子程序,为了确保正确的返回量在switch语句的最后使用了[COLOR="Red"]otherwise break[/COLOR]语句,但是程序执行时总是提醒我break出现在循环外,让我用return代替(A BREAK statement appeared outside of a loop. Use RETURN instead),可是我用return之后程序又陷入无限循环中,无法得到正确结果,请问这是什么原因造成的啊?

watcher 2008-04-24 13:55

从这个程序看,return和无限循环没有关系,导致死循环的应该是调用metric的程序


所有时间均为北京时间。现在的时间是 03:09

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.