0

ゲームでluaベースのコンピューター用のプログラムを作成しようとしています。実行すると奇妙な動作をしますが

--Tablet

    oldpullEvent = os.pullEvent
    os.pullEvent = os.pullEventRaw
    while true do
        term.clear()
        term.setTextColor( colors.white )
        term.setCursorPos(1, 1)
        print("Please Enter Password:")
        input = read("*")
        incorrect = 0
        while incorrect < 3 do
            if input == "qwerty" then
                print("Password Correct, Unlocking")

            else
                if incorrect < 3 then
                    incorrect = incorrect + 1
                    print("Password incorrect")
                    print(3 - incorrect, " tries remaining")
                else 
                    print(3 - incorrect, "tries remaining, locking phone for 1m")
                    local num = 0
                    while num < 60 do
                        if num < 60 then 
                            term.clear()
                            term.setTextColor( colors.red )
                            term.setCursorPos(1, 1)
                            num = num + 1
                            print(60 - num, "s remaining")
                            sleep(1)
                        else
                            incorrect = 0
                        end
                    end
                end
            end
        end 
    end
    os.pullEvent = oldpullEvent

実行すると、「パスワードを入力してください:」で始まり、必要なパスワードを「qwerty」で入力すると、「パスワードの修正、ロック解除」を無限にループします。間違ったパスワードを入力すると、else ステートメントのコードは実行されず、パスワードの入力画面に戻ります。エラーコードやクラッシュはありません。lua を知っている人なら、while/if/elseif 関数を間違って書いたのか、回避策を書いたのか知​​っていますか?

ありがとう!

4

2 に答える 2

1

正しいパスワードが入力されると、ループは停止するように指示されません。正しいパスワードを入力した後、break後に配置する必要がありますprint("Password Correct, Unlocking")

これはinput、ループの外側にあるためです。より良いアプローチは次のようなものです。

local incorrect = 0
while true do
    term.clear()
    term.setTextColor( colors.white )
    term.setCursorPos(1, 1)
    print("Please Enter Password:")
    local input = read("*")

    if input == "qwerty" then
        print("Password Correct, Unlocking")
        break
    else
        if incorrect < 2 then
            incorrect = incorrect + 1
            print("Password incorrect")
            print(3 - incorrect, " tries remaining")
            sleep(1) -- let them read the print.
        else
            print("out of attempts, locking phone for 1m")
            for i = 10, 1, -1 do
                term.clear()
                term.setTextColor( colors.red )
                term.setCursorPos(1, 1)
                print(i, "s remaining")
                sleep(1)
            end
            incorrect = 0
        end
    end
end

上記のコードは、ユーザーがパスワードを 3 回試行できるようにします。すべて使用すると、60 秒間ロックアウトされ、さらに 3 回試行されます。これは、正しいパスワードが入力されるまで繰り返されます。

内側の while ループは不要だったので削除しました。はincorrectローカルになり、while ループの外に移動されたため、ユーザーがパスワードを入力するたびにリセットされることはありません。

が while ループ内に移動されたread("*")ため、パスワードを 1 回要求してから無限にループするのではなく、毎回ユーザーにパスワードの入力を求めるようになりました。

コードはテスト済みで、問題なく動作するようです。

コードのいずれかが意味をなさない場合は、遠慮なく質問してください。

于 2016-01-10T06:44:21.457 に答える
0

incorrect正しいパスワードが入力された後、値をリセットしません。を使用breakしてループを中止するかincorrect、3 以上の値に設定する必要があります。

于 2015-12-12T23:12:46.840 に答える