这里是Ruby新手:需要使用给定Integer打印单面金字塔的帮助



输入整数6

输出:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

尝试循环,但未能获得输出。

def pyramid(i)
  (1..i).each do |n|
      1.upto(n) do |x|
        print "#{x} " # Mind double quotes
      end
      puts
  end
end
pyramid(6)

阅读更多关于Ruby I/O的信息

阅读更多关于Ruby中的循环

最新更新