2017-11-25 16 views
1

나는 matlab에 간단한 암호 프로그램을 만들었습니다. 아시다시피 일반적인 암호 프로그램은 암호를 3 회 잘못 입력하면 프로그램이 잠긴 것입니다. 오류. 속성을 추가하려면 어떻게해야합니까?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

대신 동안의 리터에서'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 개의 손가락을 주셨고 두 줄의 코드를 지우려면 그 중 하나만 있으면됩니다. –