17

私の lua プログラムでは、操作を続行する前に停止してユーザーに確認を求めたいと考えています。停止してユーザー入力を待つ方法がわかりません。どうすればできますか?

4

6 に答える 6

30
local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"
于 2009-11-29T10:30:33.140 に答える
15

ライブラリを見てみましょうio。これには、デフォルトで標準入力がデフォルトの入力ファイルとして含まれています。

http://www.lua.org/pil/21.1.html

于 2009-11-29T10:19:27.963 に答える
9

私はこのようなコードで作業しました。私はそれが機能する方法でこれを入力します:

io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
   --(put what you want it to do if you say y here)
elseif answer=="n" then
   --(put what you want to happen if you say n)
end
于 2012-12-12T23:11:30.170 に答える
1

私が使う:

     print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end
于 2013-02-13T22:40:50.063 に答える
1

次のコードを使用してみてください

m=io.read() if m=="yes" then (insert functions here) end

于 2013-04-19T14:59:49.890 に答える
-1
print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end

私が行ったちょっとした lua から (多くはない)、string.sub を使用する場合、大文字と小文字の両方を使用することは冗長であると言うつもりです。

print("Continue? (y/n)")
local re = io.read()

--[[Can you get string.sub from a local var? 
If so, this works. I'm unfamiliar with io(game 
lua uses GUI elements and keypresses in place of the CLI.]]

if re.sub == "y" then
    --do stuff
if re.sub == "n" then
    --do other stuff
end

それはうまくいくはずです。

于 2013-12-20T17:54:38.017 に答える