2015-08-28 44 views
0

我想學習如何編程Computercraft Mining Turtles,我想編寫一個程序,提示用戶輸入一個立方體的邊長,然後再建立。我使用io.read()讓用戶輸入邊長,如果他希望立方體通過Y/N輸入變爲實心或空心。然而,當我檢查最後一個問題的答案時,我得到一個錯誤,我有一個表並需要一個字符串。 錯誤代碼:立方:17:壞的說法:字符串預期,拿到表如何使io.read()返回一個字符串?

這裏是我的代碼

--"Cube constructor" 
--"Powered by UglySoft" 

function get_dimensions() 
    --"prompts for cube dimensions and Solid" 

    print("Please enter the Cube dimensions.\n") 

    io.write("Cube side length ") 
    side_length = tonumber(io.read()) 

    io.write("Solid Cube? (Y/N) ") 
    solid = io.read() 


    solid = string:lower() 

    return side_length, solid 
end 

function build_solid(side_length) 
    print("Building solid Cube with side length ", side_length) 
end 

function build_hollow(side_length) 
    print("Building hollow Cube with side length ", side_length) 
end 


function main() 
    --"main part of the program" 
    term.clear() 
    print("Welcome to Cube Builder") 
    print("powered by UglySoft \n") 


    get_dimensions() 

    if solid == "y" then 
     build_solid() 
    else 
     build_hollow() 
    end 

end 


main() 

我很新的Lua和我甚至不知道我是否應該使用io.read( ) 在這個情況下。任何幫助非常感謝,並隨時提問。

回答

1

您的問題是string:lower(),它將脫糖至string.lower(string),它嘗試在string表上運行。這是行不通的。

你想要solid:lower()string.lower(solid)

+1

此外,他還需要從build_solid()和build_hollow()中的參數列表中刪除side_length,它隱藏全局函數,或者在調用其中一個函數時將其傳入。顯然,OP需要更好地理解一般的編程概念。 – tonypdmtr

0

在ComputerCraft你通常應該使用一個內置功能

read() 

代替io.read因爲io.read是閱讀,而不是用戶輸入文件進行。可以使用writeprint

相關問題