If statement Lua elseif工作不正常
当我尝试使用If statement Lua elseif工作不正常,if-statement,lua,If Statement,Lua,当我尝试使用elseif时,它不起作用。对于下面的代码,无论用户输入的是哪一个数字,运行的唯一代码都是if语句下的代码 io.write("do you want to convert from celsius to farenheit (1), or the other way around (2)?") pref = io.read() if pref == 1 then io.write("Hi there! what's your temperature in celsius? ")
elseif
时,它不起作用。对于下面的代码,无论用户输入的是哪一个数字,运行的唯一代码都是if语句下的代码
io.write("do you want to convert from celsius to farenheit (1), or the other way around (2)?")
pref = io.read()
if pref == 1 then
io.write("Hi there! what's your temperature in celsius? ")
local cel = io.read()
far = (cel*1.8+32)
io.write("That temperature in farenheit is: " .. far)
elseif pref == 2 then
io.write("Hi there! what's your temperature in farenheit? ")
local far = io.read()
cel = ((far-32)/1.8)
print("That temperature in celsius is: " .. cel)
end
问题不在于
elseif
。问题是因为io.read()
返回一个字符串。将其转换为数字:
pref = tonumber(io.read())
或者,将
pref
与“1”
,“2”
进行比较。调试器/IDE将帮助查看正在执行的代码以及Lua值的类型和值。