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使用したステートメントを削除し、 の後に閉じるだけにします。ifelseifelse

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もありませんelseendそして、 after the elsepart と beforeがあるはずですuntil:

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

次回は、少なくとも問題の内容 (エラー メッセージ、予期しない出力など) を投稿していただけると助かります。

于 2013-03-22T00:49:37.333 に答える