我制作了一个 lua 计算器来学习语言,但是当我 运行 代码时它会跳过 io.read
I have made a lua calculator to learn the language but when i run the code it skips an io.read
function CALC()
print("First number:")
local input1 = io.read("*n")
print("operator: ")
local operator = io.read("*l")
print("secondNumber:")
local input2 = io.read("*n")
local switchCalc = {
["+"] = function ()
local result = input1 + input2
print(result)
end,
["-"] = function ()
local result = input1 - input2
print(result)
end,
["*"] = function ()
local result = input1 * input2
print(result)
end,
["/"] = function ()
local result = input1 / input2
print(result)
end
}
local a = 1
local f = switchCalc[a]
if(f) then
f()
else
print("Default")
end
end
CALC()
我的输出是:
第一个数字:10
运算符:
第二个号码:
*
默认
希望有人能帮我解决这个问题,因为我已经主演了几个小时了,我真的不明白
我知道多少:
print("First number:") -- prints -> First number:
local input1 = io.read("*n") -- when you write 10 , buffer got "10(enter)"
-- now buffer is "(enter)"
print("operator: ") -- prints -> operator:
local operator = io.read("*l") -- buffer not empty then operator is "(enter)"
print("secondNumber:")
local input2 = io.read("*n") -- try to read "*"
解决方案是在 print("operator: ")
之前添加 io.read("*l")
或使用 io.read() 而不是 io.read("*n")
/ io.read("*l")
我建议不带任何参数地使用 io.read()。
如果使用 io.write()
它看起来更像是一个提示
但是将它转换成即时要求的东西...
-- calc.lua (Tested with Lua 5.4)
local function calc()
io.write("First number: ") local input1 = tonumber(io.read()) -- number
io.write("Operator: ") local operator = io.read() -- Let a string be a string
io.write("Second number: ") local input2 = tonumber(io.read()) -- number
print('Equals to:', load('return '.. input1 .. operator .. input2)())
end
return calc
比...
> calc = require('calc')
> calc()
First number: 3
Operator: *
Second number: 3
Equals to: 9
问题不是您认为的“代码跳过 io.read
”——只是您没有正确使用变量; 读数很好。
local f = switchCalc[a]
行使用了一个全局变量 a
,您还没有定义它(当然也不是运算符)。因此它将是 nil
。 switchCalc[nil]
又是 nil
,所以 f
将是 nil
。现在 if
不触发,输入 else
并且您得到“默认”作为输出。解决方法很简单:只需要 switchCalc[operator]
.
两个注意事项:
- 如果使用间距,则不需要在
if
两边加上括号;
io.read()
等同于io.read("*l")
(可以省略"*l"
)
function CALC()
print("First number:")
local input1 = io.read("*n")
print("operator: ")
local operator = io.read("*l")
print("secondNumber:")
local input2 = io.read("*n")
local switchCalc = {
["+"] = function ()
local result = input1 + input2
print(result)
end,
["-"] = function ()
local result = input1 - input2
print(result)
end,
["*"] = function ()
local result = input1 * input2
print(result)
end,
["/"] = function ()
local result = input1 / input2
print(result)
end
}
local a = 1
local f = switchCalc[a]
if(f) then
f()
else
print("Default")
end
end
CALC()
我的输出是:
第一个数字:10
运算符:
第二个号码:
*
默认
希望有人能帮我解决这个问题,因为我已经主演了几个小时了,我真的不明白
我知道多少:
print("First number:") -- prints -> First number:
local input1 = io.read("*n") -- when you write 10 , buffer got "10(enter)"
-- now buffer is "(enter)"
print("operator: ") -- prints -> operator:
local operator = io.read("*l") -- buffer not empty then operator is "(enter)"
print("secondNumber:")
local input2 = io.read("*n") -- try to read "*"
解决方案是在 print("operator: ")
之前添加 io.read("*l")
或使用 io.read() 而不是 io.read("*n")
/ io.read("*l")
我建议不带任何参数地使用 io.read()。
如果使用 io.write()
它看起来更像是一个提示
但是将它转换成即时要求的东西...
-- calc.lua (Tested with Lua 5.4)
local function calc()
io.write("First number: ") local input1 = tonumber(io.read()) -- number
io.write("Operator: ") local operator = io.read() -- Let a string be a string
io.write("Second number: ") local input2 = tonumber(io.read()) -- number
print('Equals to:', load('return '.. input1 .. operator .. input2)())
end
return calc
比...
> calc = require('calc')
> calc()
First number: 3
Operator: *
Second number: 3
Equals to: 9
问题不是您认为的“代码跳过 io.read
”——只是您没有正确使用变量; 读数很好。
local f = switchCalc[a]
行使用了一个全局变量 a
,您还没有定义它(当然也不是运算符)。因此它将是 nil
。 switchCalc[nil]
又是 nil
,所以 f
将是 nil
。现在 if
不触发,输入 else
并且您得到“默认”作为输出。解决方法很简单:只需要 switchCalc[operator]
.
两个注意事项:
- 如果使用间距,则不需要在
if
两边加上括号; io.read()
等同于io.read("*l")
(可以省略"*l"
)