Ruby中的金字塔模式需要一些代码更正
Ruby中的金字塔模式需要一些代码更正,ruby,Ruby,使您的代码满足您需要的最小修改量: i=1 k=5 j=1 t=1 t=k i.upto(k) do j.upto(t) do print " " end t-=1 j.upto(2*i-1) do print "*" end j+=1 print "\n" i+=1 end 一个字符值对其他程序员不好 字符串可以使用*运算符相乘 puts函数打印带换行符的字符串。 put
使您的代码满足您需要的最小修改量:
i=1
k=5
j=1
t=1
t=k
i.upto(k) do
j.upto(t) do
print " "
end
t-=1
j.upto(2*i-1) do
print "*"
end
j+=1
print "\n"
i+=1
end
一个字符值对其他程序员不好 字符串可以使用*运算符相乘 puts函数打印带换行符的字符串。 puts提供与print相同的结果\n upto不太好,使用次数或每一次都很直观 a+=1不酷,保持修改值应为最小值
i=1
k=5
t=5
i.upto(k) do
t.times do
print ' '
end
(2 * i - 1).times do
print '*'
end
print "\n"
t -= 1
i += 1
end
遗嘱提供
def pyramid(height)
height.times {|n|
print ' ' * (height - n)
puts '*' * (2 * n + 1)
}
end
pyramid 5
def pyramid(height)
height.times {|n|
print ' ' * (height - n)
puts '*' * (2 * n + 1)
}
end
pyramid 5
*
***
*****
*******
*********