MATLAB - недвижимость

Я создал простую программу паролей с помощью Matlab. Как вы знаете, программы для типичных паролей имеют простую идею: если вы введете неправильный пароль три раза, программа отправит вам LOCKED! ошибка. так как я могу добавить свойство?

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

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...

Вы можете поместить счетчик, который вы повторяете в цикле:

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...
Другие вопросы по тегам