你可以把你迭代循環內的計數器:
locked_flag = false
counter = 0
while (x ~= 1111)
if (x == password)
uiwait(msgbox('Welcome!'));
break;
else
uiwait(errordlg('Try Again!!'));
counter = counter + 1
if (counter == 3)
locked_flag = true;
%show 'locked' dialog of some kind here
break;
end
X = inputdlg('Please enter your password');
x = str2double (X{1,1});
end
end
%can now check if locked_flag true to start unlock logic or other...
編輯:是的,我只希望你張貼代碼片段,你需要它上面的邏輯,這樣,如果對不起不清楚:
X = inputdlg ('Please enter your password');
x = str2double (X {1,1});
password = 1111; %whatever
if (x == password)
uiwait(msgbox('Welcome!'));
else
locked_flag = false
counter = 1
while (x ~= 1111)
if (x == password)
uiwait(msgbox('Welcome!'));
break;
else
uiwait(errordlg('Try Again!!'));
counter = counter + 1
if (counter == 3)
locked_flag = true;
%show 'locked' dialog of some kind here
break;
end
X = inputdlg('Please enter your password');
x = str2double (X{1,1});
end
end
end
%can now check if locked_flag true to start unlock logic or other...
你是相當接近,而不是在而L使用'x' oop,使用一個變量來計數。初始化如下'failed_count = 0',然後運行'while(failed_count