لوا - الحصول على المدخلات سطر الأوامر من المستخدم؟

StackOverflow https://stackoverflow.com/questions/1815163

  •  06-07-2019
  •  | 
  •  

سؤال

في برنامج لوا بلدي، وتريد أن تتوقف وتسأل المستخدم لتأكيد قبل الشروع في هذه العملية. أنا لست متأكدا من كيفية التوقف والانتظار لمدخلات المستخدمين، كيف يمكن أن يتم ذلك؟

هل كانت مفيدة؟

المحلول

ونلقي نظرة على المكتبة io، الذي افتراضيا لديها مستوى المدخلات مثل ملف الإدخال الافتراضية:

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

نصائح أخرى

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

ولقد عملت مع رمز من هذا القبيل. وسوف اكتب هذا في الطريقة التي ستعمل:

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

وأنا استخدم:

     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

ومحاولة استخدام التعليمات البرمجية التالية

و 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

ومن بت من لوا التي قمت بها (وليس كثيرا)، وأنا أريد أن أقول أن استخدام الرسائل كلا الكبيرة والصغيرة لا لزوم لها إذا كنت تستخدم 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

وهذا يجب أن تعمل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top