你能建议一下吗!场景如下:我尝试了多个switch、case语句,但无法执行..
switch 1
case 'YESS'
{
% curly braces are just to denote the scope of case 'YESS'
.....code
switch 2
case 'Yes'
from here Can I jump again to the start of switch(1),case 'YESS' ??
case 'No'
%% some message
end
}
case'NOO'
%% some message
end发布于 2017-04-26 00:11:50
但是你没有解释清楚你要找的是什么,使用下面的函数如何:
function main
prompt = 'Do you want more? Y/N [Y]: ';
str1 = askYesNoQuestion(prompt);
switch str1
case 'Y'
prompt2 = 'Asking to make sure? Y/N [Y]: ';
str2 = askYesNoQuestion(prompt2);
disp(str2);
case 'N'
disp('OK no problem!');
end
end
function str = askYesNoQuestion(prompt)
str = input(prompt,'s');
if isempty(str)
str = 'Y';
end
switch str
case 'Y'
disp('you said yes');
case 'N'
disp('you said no')
end
end您可以将整个代码保存在名为main.m的m文件中并运行它。
发布于 2017-04-26 00:13:08
你需要告诉matlab保持循环
keepLooping = true;
while keepLooping
switch 1
case 'YESS'
keepLooping = false; %% exit switch 1
switch 2
case 'Yes' %% back to switch 1
keepLooping = true; %% re-enter switch 1
case 'No' %% some message
keepLooping = false %% exit switch 1
end
case'NOO' %% some message
keepLooping = false; %% exit switch 1
end
end或者,在'YESS‘中已经跳过了:
isYesOrNo = 'YESS'
keepLooping = true;
while keepLooping
switch isYesOrNo
case 'YESS'
keepLooping = false; %% exit switch 1
switch 2
case 'Yes' %% back to switch 1
keepLooping = true; %% re-enter switch 1
isYesOrNo = 'YESS' %% re-enter 'YESS'
case 'No' %% some message
keepLooping = false %% exit switch 1
end
case'NOO' %% some message
keepLooping = false; %% exit switch 1
end
endhttps://stackoverflow.com/questions/43615701
复制相似问题