2009-11-29 22 views

Respuesta

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

awesome, thank you –

+0

¿'io.read()' impone automático 'io.flush()' cuando se trabaja con stdin/out predeterminado? –

+0

@EgorSkriptunoff, podría ser, pero no podemos estar seguros. No creo que ANSI C diga nada sobre esto. – lhf

7

He trabajado con código como este. Voy a escribir esto de una manera que va a 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 
1

que utilizo:

 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 
1

intenta utilizar código folowing

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

-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 

Por el bit de lua que he hecho (no mucho), voy a decir que usar letras mayúsculas y minúsculas es redundante si usa 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 

Eso debería funcionar.

+0

're.sub' se resolverá con la función' string.sub' y siempre será desigual a '" y "' o '" n "'. Además, la coincidencia de cadenas distingue mayúsculas de minúsculas En el mejor de los casos puedes hacer 're: match (" [nN] ")' y 're: match (" [yY] ")' –

Cuestiones relacionadas