如何使用Ruby如下打印矩形:
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
在这种情况下,行和列的长度相同,并且必须为奇数。
示例:
r =矩形(5)
应打印:
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
,如果:
r =矩形(7)
应打印:
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
谢谢。
答案 0 :(得分:4)
我将从创建以下两行开始:(有很多方法可以实现这一点)
size = 7
a = Array.new(size, '*').fill('=', 1..-2).join(' ') #=> "* = = = = = *"
b = Array.new(size, '*').join(' ') #=> "* * * * * * *"
然后我要定义一个重复模式:
pattern = [a, a, b].cycle
最后,我将打印图案 size 次:
puts pattern.take(size)
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
答案 1 :(得分:1)
[我自己解决]:D
def square(n)
begin
if n.odd?
1.upto(n) do | row |
if row % 3 != 0
puts "#{'*'} #{'= ' * (n - 2)}#{'*'}"
else
puts "#{'* ' * n}"
end
end
else
puts 'Must odd number!'
end
rescue
puts 'Must integer number!'
end
end
square(5)
输出:
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
square(7)
输出:
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
square(8)
输出:
Must odd number!
square(8.5)
输出:
Must integer number!
square('blabla')
输出:
Must integer number!
答案 2 :(得分:1)
def rectangle(n)
puts("-----------------For #{n}--------------------")
if n % 2 == 1
for i in (1..n)
for j in (1..n)
if j == 1 || j == n || 0 == i % 3
print "* "
else
print "= "
end
end
print("\n")
end
end
end
rectangle(3)
rectangle(5)
rectangle(7)
rectangle(9)
rectangle(11)
-----------------For 3--------------------
* = *
* = *
* * *
-----------------For 5--------------------
* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
-----------------For 7--------------------
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
-----------------For 9--------------------
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
-----------------For 11--------------------
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
* * * * * * * * * * *
* = = = = = = = = = *
* = = = = = = = = = *
答案 3 :(得分:1)
只是为了好玩修改@spn答案
def rectangle(count)
return 'Must odd number more than 1' unless count.is_a?(Integer) && count.odd? && count > 1
Array.new(count) { |index| (index + 1) % 3 == 0 ?
"#{'* ' * count}".chomp(" ") :
"#{'*'} #{'= ' * (count - 2)}#{'*'}" }.join("\n")
end
现在
puts rectangle(2) # will print Must odd number more than 1
puts rectangle("asdf") # will print Must odd number more than 1
puts rectangle(9) # will print:
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
* = = = = = = = *
* = = = = = = = *
* * * * * * * * *
最好避免使用方法中的puts
。因此,您可以在网络,电报机器人等中再次使用它们。同样也不用用自己(但相同)的消息来复制Ruby异常的方法。
答案 4 :(得分:0)
我们首先定义一种构造两种类型的线的方法。
def make_line(n, mid_char)
['*', *[mid_char]*(n-2), '*'].join(' ')
end
make_line(5, '*') #=> "* * * * *"
make_line(5, '#') #=> "* # # # *"
现在创建一种方法以所需的图案绘制线条。仅当i
等于零时,第(i+1) % 3
行(基数为零)由星和空格组成;否则它也是包含英镑符号的行。
def draw(n)
all_stars = make_line(n, '*')
two_stars = make_line(n, '#')
n.times { |i| puts ((i+1) % 3).zero? ? all_stars : two_stars }
end
draw 5
* # # # *
* # # # *
* * * * *
* # # # *
* # # # *
draw 6
* # # # # *
* # # # # *
* * * * * *
* # # # # *
* # # # # *
* * * * * *
draw 7
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *