当我尝试使用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
with "1"
, "2"
反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)