2009-11-29 17 views

risposta

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

impressionante, grazie –

+0

'io.read()' impone automatico 'io.flush()' quando si lavora con default stdin/out? –

+0

@EgorSkriptunoff, potrebbe, ma non possiamo esserne sicuri. Non credo che ANSI C ne dica nulla. – lhf

7

Ho lavorato con codice come questo. Io digitare questo in un modo che funzionerà:

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

io 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 
1

tenta di utilizzare il codice 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 

Dal bit di lua che ho fatto (non molto), sto per dire che usare lettere maiuscole e minuscole è ridondante se 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 

Che dovrebbe funzionare.

+0

're.sub' si risolverà nella funzione' string.sub' e sarà sempre diseguale in '" y "' o '" n "'. Inoltre, la corrispondenza delle stringhe è case sensitive. Nella migliore delle ipotesi puoi fare 're: match (" [nN] ")' e 're: match (" [yY] ")' –

Problemi correlati