如何打印星号和散列的模式,以便在lua编程中四个星号之后应该有一个散列

如何打印星号和散列的模式,以便在lua编程中四个星号之后应该有一个散列,lua,Lua,模式应该是这样的: ****#****#**** ****#****#**** -- generatePattern function to print the pattern of start(*) and hash(#) function generatePattern() --n = tonumber(io.read()) for i=14, 1, -1 do for j=1, i, 1 do io.write("*&qu

模式应该是这样的:

****#****#****
****#****#****
-- generatePattern function to print the pattern of start(*) and hash(#)
function generatePattern()
    --n = tonumber(io.read())
    for i=14, 1, -1 do
        for j=1, i, 1 do
            io.write("*")
            if (i==5) then
                char = "*"
                newchar=char.gsub(string,"#")
                print("#")
            end
        end
        print("  ")
    end
end

-- read the test cases input
tc = tonumber(io.read())

-- for each case, call the generatePattern function to print the pattern
for i=1,1 do
    generatePattern()
end
我试着这样做:

****#****#****
****#****#****
-- generatePattern function to print the pattern of start(*) and hash(#)
function generatePattern()
    --n = tonumber(io.read())
    for i=14, 1, -1 do
        for j=1, i, 1 do
            io.write("*")
            if (i==5) then
                char = "*"
                newchar=char.gsub(string,"#")
                print("#")
            end
        end
        print("  ")
    end
end

-- read the test cases input
tc = tonumber(io.read())

-- for each case, call the generatePattern function to print the pattern
for i=1,1 do
    generatePattern()
end

让我们看看你的代码是做什么的

-- define a function generatePattern
function generatePattern()

    -- loop 14 times, reducing i = 14 by 1 each turn
    for i=14, 1, -1 do
        -- loop i times
        for j=1, i, 1 do
            -- output a star
            io.write("*")
            -- if i == 5 (this happens exactly once!)
            if (i==5) then
                -- create a global variable char that stores a string "*"
                char = "*"
                -- cause a lua error for using the string libraries table as a literal string
-- looks like you tried to replace something with a hash
                newchar=char.gsub(string,"#")
                -- print a hash
                print("#")
            end
        end
        print("  ")
    end
end

-- read a value you don't use
tc = tonumber(io.read())

-- call your function in a loop
for i=1,1 do
    generatePattern()
end
我不知道你是怎么想出这个逻辑的。请用笔和纸写下为了达到你的目标需要做些什么

您当然不想打印14个星号,然后是13个星号,一直打印到5个星号,然后再打印一个散列。这完全没有意义(无意冒犯!)

那么我们需要做什么呢?我们要打印
“*********************”

简单的方式显然是
print(************************”)

创建一个函数来生成这个字符串只有在有任何参数会改变这个字符串时才有意义。喜欢许多散列,因此您可以轻松打印
“*****”
“*****”************和其他内容

那么,为什么不简单地将散列数作为输入呢

function generatePattern(numHashes)
  numHashes = numHashes or 0
  local output = ""
  local numCharacters = numHashes + 4 + numHashes * 4
  for i = 1, numCharacters do
    -- append a # if i is a multiple of 5, append a * else
    output = output .. (i % 5 == 0 and "#" or "*")
  end
  return output
end

-- test 
for i = 0, 5 do
  print(generatePattern(i))
end
印刷品:

****
****#****
****#****#****
****#****#****#****
****#****#****#****#****
****#****#****#****#****#****
如果您还想调整星号的数量,请执行以下操作:

function generatePattern(numHashes, numAsterisks)
  numHashes = numHashes or 0
  numAsterisks = numAsterisks or 4
  local output = ""
  
  local numCharacters = numHashes + numAsterisks + numHashes * numAsterisks
  for i = 1, numCharacters do
    output = output .. (i % (numAsterisks + 1) == 0 and "#" or "*")
  end
  return output
end

-- test 
for i = 1, 5 do
  print(generatePattern(i, 6))
end

看看这个答案:。你能解释一下你的代码应该做什么吗?因为我不知道你是怎么想出这样的东西来获得想要的输出的。