Lua io.read()不是';行不通

Lua io.read()不是';行不通,lua,Lua,我刚开始编写一个非常基本的计算器程序,它工作得很好,但我无法让它在完成计算后保持程序打开。io.read()似乎不起作用。我的代码如下: promptmethod = "Would you like to use addition, subtraction, multiplication, or division?" promptnumber1 = "Enter a number:" promptnumber2 = "Enter another number:" anothe

我刚开始编写一个非常基本的计算器程序,它工作得很好,但我无法让它在完成计算后保持程序打开。io.read()似乎不起作用。我的代码如下:

promptmethod = "Would you like to use addition, subtraction, multiplication, or           division?"
promptnumber1 = "Enter a number:"
promptnumber2 = "Enter another number:"
anotherprompt = "Would you like to make another calculation?"

print(promptmethod)
usermethod = io.read("*line")
print(promptnumber1)
number1 = io.read("*number")
print(promptnumber2)
number2 = io.read("*number")

if usermethod == "addition" then
answer = number1 + number2
stringanswer = "Your calculation is " .. number1 .. " + " .. number2 .. " = " .. answer
elseif usermethod == "subtraction" then
answer = number1 - number2
stringanswer = "Your calculation is " .. number1 .. " - " .. number2 .. " = " .. answer
elseif usermethod == "multiplication" then
answer = number1 * number2
stringanswer = "Your calculation is " .. number1 .. " × " .. number2 .. " = " .. answer
elseif usermethod == "division" then
answer = number1 / number2
stringanswer = "Your calculation is " .. number1 .. " ÷ " .. number2 .. " = " .. answer
else
error("Invalid operation or values.")
end

print(stringanswer)

io.read()
有人知道为什么会这样吗?谢谢大家!

一个简单的例子:

io.read('*number')
io.read()
问题是
read('*number')
不使用数字后面的换行符。因为它仍然位于输入流上,所以当您调用
io.read
(默认为读取一行)时,它会立即返回

要解决这个问题,只需再次调用
io.read