Pergunta

No meu programa lua, eu quero parar e perguntar utilizador para confirmação antes de prosseguir com uma operação. Não tenho certeza como parar e aguardar a entrada do usuário, como pode ser feito?

Foi útil?

Solução

Dê uma olhada na biblioteca io, que por padrão tem padrão de entrada como o arquivo de entrada padrão:

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

Outras dicas

local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"

Eu trabalhei com um código como este. Vou escrever isso de uma maneira que vai funcionar:

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

Eu uso:

     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

tentar usar seguinte código

m=io.read() if m=="yes" then (insert functions here) 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

Desde o pouco de lua que eu fiz (não muito), eu vou dizer que o uso de ambos os maiúsculas e minúsculas é redundante se você usar 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

Isso deve funcionar.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top