Lua-ユーザーからコマンドライン入力を取得しますか?
-
06-07-2019 - |
質問
私のluaプログラムで、操作を続行する前に停止してユーザーに確認を求めます。ユーザー入力を停止して待機する方法がわかりません。どうすればよいですか?
解決
io
ライブラリをご覧ください。デフォルトでは、標準入力ファイルとして標準入力があります:
他のヒント
local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
このようなコードを使用しました。動作するようにこれを入力します:
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
使用:
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
次のコードを使用してみてください
m = io.read()
if m ==" yes"
(ここに関数を挿入)
end
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
これでうまくいくはずです。
所属していません StackOverflow