2013-03-15 85 views
0

我在做這個Lua代碼有什麼問題? 我正在嘗試生成兩個隨機數,並詢問它們在一起倍增。 第一部分是好的,但不管我輸入什麼答案,它總是認爲它是不正確的。 請告訴我我做錯了什麼,以及如何解決它。請記住這是Lua。Lua和用戶輸入

math.randomseed(os.time()) 
local a=math.random(10) 
local b=math.random(10) 


local answer 
repeat 
io.write("What is ",a,"*",b,"?")  
io.flush() 
answer=io.read() 
if answer==a*b then 
print("Correct!") 
else 
print("Try Again") 
end 
until 
answer==a*b 

回答

4

io.read()返回string,而你與一些比較吧。

您或者需要說answer=tonumber(io.read()),或者您需要說io.read("*n")

+0

謝謝凱文。非常有幫助 – user2059810 2013-03-15 23:08:04

+2

@ user2059810歡迎來到stackoverflow!考慮接受凱文的答案(通過點擊複選標記)給他一些榮譽,並向未來的訪問者展示你的問題已經解決(以及如何) – 2013-03-16 00:00:12