Domanda

Nel mio programma lua, voglio fermarmi e chiedere conferma all'utente prima di procedere con un'operazione. Non sono sicuro di come fermare e attendere l'input dell'utente, come può essere fatto?

È stato utile?

Soluzione

Dai un'occhiata alla libreria io , che per impostazione predefinita ha l'input standard come file di input predefinito:

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

Altri suggerimenti

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

Ho lavorato con codice come questo. Scriverò 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

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

prova a usare il seguente codice

m = io.read () se m == " yes " il (inserire le funzioni qui) 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

Dal bit di lua che ho fatto (non molto), dirò che l'uso di lettere maiuscole e minuscole è ridondante se si utilizza 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

Dovrebbe funzionare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top