Question

Dans mon programme lua, je souhaite arrêter et demander une confirmation à l’utilisateur avant de procéder à une opération. Je ne sais pas comment arrêter et attendre les commentaires des utilisateurs, comment peut-on le faire?

Était-ce utile?

La solution

Examinez la bibliothèque io , qui a par défaut une entrée standard comme fichier d'entrée par défaut:

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

Autres conseils

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

J'ai travaillé avec un code comme celui-ci. Je vais taper ceci d'une manière qui fonctionnera:

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

j'utilise:

     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

essayez d'utiliser le code suivant

m = io.read () si m == " oui " le (insérer les fonctions ici) fin

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

Sur le peu de Lua que j’ai fait (pas beaucoup), je vais dire que l’utilisation de lettres majuscules et minuscules est redondante si vous utilisez 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

Cela devrait fonctionner.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top