0

我的 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"
4

3 回答 3

3

if要先关闭你的街区。删除end你用来关闭if和的语句,elseif并将其放在后面关闭else

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"

PS:这可能会导致您进入无限循环。您需要which在重复直到每次迭代后更新。

于 2013-03-22T00:51:59.843 回答
1

end之前应该没有elseif。也应该没有endbefore 和没有doafter else。并且应该有一个end之后else和之前的部分until

repeat
  if ... then
    ...
  elseif ... then
    ...
  else
    ...
  end
until ...

下次如果您至少发布您的问题是什么(错误消息、意外输出等)会很有帮助。

于 2013-03-22T00:49:37.333 回答
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"
于 2018-02-28T19:46:18.890 回答