Lua的一些Demo

排列大小

test = {2,453,223,76,888,90,323,4345,5,66,112,231}

i=1
j=1
while i <(#test) do    --外循环,需要把所有数字都比较一次,共#test-1次
    while j<=(#test-i) do   --内循环,每比较一个数字后,下一个都会少一次
        if test[j] < test[j+1]then  --比较大小
            test[j],test[j+1]=test[j+1],test[j]   --替换位置
        end
        j=j+1
    end
    j=1
    i=i+1
end


for k,v in pairs(test) do  --遍历出来
    print(v)

end

找出100被7整出的数

i=7
while i<=100 do
if i%7==0 then
    print(i)
end
i=i+1

end

乘法口诀

--第一种写法
function chengfa1()
    for a = 1, 9 do
        local c = ""
        for b = 1, 9 do
            if a>=b then
                c = c..b.."x"..a.."="..a*b.."\t"
            end
            end
        print(c)

    end
end


--第二种写法
function chengfa2()  --创建函数
    for a = 1,9 do    --定义一个循环1到9
        local s = ""  ---- 定义局部变量s
        for b=1,9 do
            if a>=b then
                s = s..string.format( "%dX%d=%d\t",a,b,a*b )--..用来连接,
            end
        end
        print(s)
    end

end


--chengfa1()
chengfa2()

猜你喜欢

转载自blog.csdn.net/qq_37489565/article/details/84071565
今日推荐