我的lua代码出了什么问题?
local which
print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
which = io.read()
repeat
if which=="f" then
local c
local f
print("input your fahrenheit temperature")
f = tonumber(io.read())
c = (f-32)/1.8
print(c)
end
elseif which=="c" then
local ce
local fa
print("input your celsius temperature")
c = tonumber(io.read())
f = (c*1.8)+32
end
else do
print("Type f to convert fahrenhiet to celsius and c to convert celsius to fahrenheit")
until which=="f" or which=="c"
答案 0 :(得分:3)
您先关闭if
块。移除您用于关闭end
和if
的{{1}}语句,并在elseif
之后将其关闭。
else
P.S。:这可能会导致您无限循环。您需要在重复内部的每次迭代后更新local which
print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
which = io.read()
repeat
if which=="f" then
local c
local f
print("input your fahrenheit temperature")
f = tonumber(io.read())
c = (f-32)/1.8
print(c)
elseif which=="c" then
local ce
local fa
print("input your celsius temperature")
c = tonumber(io.read())
f = (c*1.8)+32
else
print("Type f to convert fahrenhiet to celsius and c to convert celsius to fahrenheit")
end
until which=="f" or which=="c"
,直到。
答案 1 :(得分:1)
end
之前应该没有elseif
。在end
之后,之前也应该没有do
而没有else
。在end
部分之后和else
之前应该有until
:
repeat
if ... then
...
elseif ... then
...
else
...
end
until ...
如果你至少发布了你的问题(错误信息,意外输出等),下次会有所帮助。
答案 2 :(得分:0)
local which
repeat
print("Type f to convert fahrenheit to celsius and c to convert celsius to fahrenheit")
which = io.read()
if which=="f" then
local c
local f
print("input your fahrenheit temperature")
f = tonumber(io.read())
c = (f-32)/1.8
print(c)
elseif which=="c" then
local c
local f
print("input your celsius temperature")
c = tonumber(io.read())
f = (c*1.8)+32
print(f)
end
print("do you want to play again? y/n?")
antwort = io.read()
until antwort ~= "y"