2013-03-22 6 views
0

무엇이 루아 코드에 문제가 있습니까?루아의 반복 루프까지 반복

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

답변

3

먼저 if 블록을 닫습니다. ifelseif을 닫고 else 다음에 닫는 데 사용한 end 문을 제거하십시오.

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" 

P.S. : 이로 인해 무한 루프가 발생할 수 있습니다. 반복 내내 반복 될 때까지 which을 업데이트해야합니다.

1

elseif 전에 더 end이 없어야합니다. else 뒤에는 do이없고 전에도 end이 없어야합니다. 그리고 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

문제가 무엇인지 간단히 설명해 주시겠습니까? –