2017-11-25 81 views
1

我用matlab创建了一个简单的密码程序。如您所知,典型的密码程序有一个简单的想法,即如果您错误地输入了三次密码,程序会向您发送LOCKED!错误。 那么我该如何添加属性?MATLAB - 属性

while (x ~= 1111) 
    if (x == password) 
     uiwait(msgbox('Welcome!')); 
     break; 
    else 
     uiwait(errordlg('Try Again!!')); 

     X = inputdlg('Please enter your password'); 
     x = str2double (X{1,1}); 
    end 
end 
uiwait(msgbox('Welcome!')); 
end 
+1

你是相当接近,而不是在而L使用'x' oop,使用一个变量来计数。初始化如下'failed_count = 0',然后运行'while(failed_count

回答

1

你可以把你迭代循环内的计数器:

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... 
2
wrong = 0; 
lock = false; 

% Keeping a char type allows you to use different 
% types of passwords (i.e. alphanumeric). 
password = '1111'; 

% Infinite loop, which allows you to implement full 
% control over the iterations. 
while (true)  
    pass = inputdlg('Please enter your password:'); 

    % If the user pushes the Cancel button, he is not inserting a 
    % wrong password, so no action should be taken towards locking. 
    % If he pushes the Ok button with empty textbox, {''} is 
    % returned, which could be wrong. 
    if (isempty(pass)) 
     continue; 
    end 

    if (strcmp(pass,password)) 
     uiwait(msgbox('Welcome!')); 
     break; 
    else 
     uiwait(errordlg('Try again!')); 
     wrong = wrong + 1; 

     if (wrong == 3) 
      lock = true; 
      break; 
     end 
    end 
end 

if (lock) 
    uiwait(errordlg('Application locked!')); 
    return; 
end 

% Your logic starts here... 
+0

没关系。但首先为什么取消按钮不起作用?还有一个简单的问题与您的锁定属性:如果您输入密码错误首先尝试再次将显示,然后应用程序锁定。你知道这个功能并不有趣。我只想显示一条消息,这是应用程序锁定! – Joe

+0

当用户点击取消按钮时,这意味着他不想插入密码......所以,从技术上讲,不应该将其视为错误的密码输入。最后但并非最不重要的一点是,如果该消息功能没有意义,只需将其删除;上帝给了你10个手指,而你只需要其中一个手指去除两行代码。 –