2013-03-22 55 views
0

我在做我的lua代碼有什麼問題?重複,直到在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

保持一致的縮進使事情更加清晰:縮進你的重複主體......這將幫助你解決很多這種類型的錯誤(儘管不完全是這個實例)。 – jpjacobs 2013-03-22 09:52:47

回答

3

您正在關閉if塊。刪除您用於關閉ifelseif的對賬單end,並在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" 

附::這可能會導致你無限循環。在重複內部的每次迭代之前,您需要更新which

1

應該有elseif之前沒有endend之前也不應該有do之後else之後。而且應該有一個endelse部分和until前:

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

下一次,如果你發佈至少你的問題是什麼(錯誤信息,意外輸出等)也將是有益的。

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" 
+0

你能簡單地描述一下問題嗎? – 2018-02-28 20:50:52