2009-11-29 86 views

回答

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

真棒,謝謝 – 2015-01-12 16:47:29

+0

當使用默認的標準輸入/輸出時,'io.read()'是否自動加入'io.flush()'? – 2015-06-10 12:40:45

+0

@EgorSkriptunoff,它可能,但我們不能確定。我不認爲ANSI C對此有任何說明。 – lhf 2015-06-10 12:47:01

7

我像這樣的代碼工作。我會在某種程度上鍵入此它將工作:

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

我用:

 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

嘗試使用如下因素代碼

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 

從我已經完成的lua(不是很多)的角度來說,如果使用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 

這應該工作。

+0

're.sub'將解析爲函數'string.sub'並且總是不等於'「y」'或'「n」'。此外,字符串匹配區分大小寫。最好你可以做're:match(「[nN]」)和're:match(「[yY]」) – 2018-01-14 18:17:12

相關問題