-- 1.宣告一個function,輸入一個正整數X,回傳X階層(X!),若輸入非正整數要報錯
-- ex:
-- input : 5
-- return : 120
-- input : 0
-- return : "Error, input value must be a positive integer"
function EX_1(x)
if(type(x) == "number" and x > 0 and x%1==0)
then
local sum = 1
local i = 1
while(x > i)
do
sum = sum*x
x = x - 1
end
print(sum)
else
print("Error, input value must be a positive integer.")
end
end
EX_1(4)
To embed this program on your website, copy the following code and paste it into your website's HTML: