创建一个三角形

时间:2016-02-02 01:00:29

标签: ruby

提问者删除了这个问题。

3 个答案:

答案 0 :(得分:0)

第一个

size = 5
size.times do |n|
  puts (1..(size-n)).to_a.join('')
end

第二个

size = 5
size.times do |n|
  puts (1..(n+1)).to_a.join('')
end

答案 1 :(得分:0)

如果您必须根据示例使用while循环,请尝试:

print "Enter a starting number: "
size = gets.to_i
line = 1

while line <= size
    count = 1
    while count <= line
        print count
        count += 1
    end
    puts
    line += 1
end

Else 尝试:

print "Enter n: "
n = gets.to_i
puts

(1..n).each do |i|
    (1..i).each {|j| print j }
    puts
end

答案 2 :(得分:0)

要按照与教师代码类似的格式,以下工作

print "Enter a starting number: "
size = gets.to_i
line = size - 1
while line >= 0
    count = 1
    while count <= size - line
        print count
        count += 1
    end
    puts
    line -= 1
end