Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 用红宝石打印矩形_Ruby - Fatal编程技术网

Ruby 用红宝石打印矩形

Ruby 用红宝石打印矩形,ruby,Ruby,如何使用Ruby打印如下矩形: * = = = * * = = = * * * * * * * = = = * * = = = * 在这种情况下,行和列的长度相同,并且必须为奇数 例如: r=矩形(5) 应打印: * = = = * * = = = * * * * * * * = = = * * = = = * 如果: r=矩形(7) 应打印: * = = = = = * * = = = = = * * * * * * * * * = = = = = * * = = = = = * * *

如何使用Ruby打印如下矩形:

* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
在这种情况下,行和列的长度相同,并且必须为奇数

例如:

r=矩形(5)

应打印:

* = = = *
* = = = *
* * * * *
* = = = *
* = = = *
如果:

r=矩形(7)

应打印:

* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
提前谢谢。

[由我解决]: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)
输出:

必须是奇数

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--------------------
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = *
square(8.5)
输出:

必须是整数

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--------------------
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = *
square('blabla')
输出:


必须是整数

只是为了好玩修改@spn答案

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--------------------
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = * 
* * * * * * * * * * * 
* = = = = = = = = = * 
* = = = = = = = = = *
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

最好避免将
放入方法中。因此,您可以在web、Telegrambot等中再次使用它们。另外,通过您自己(但相同)的消息复制Ruby异常也不是一个好主意。

我首先创建两行:(有很多方法可以实现这一点)

然后我将定义一个重复模式:

pattern = [a, a, b].cycle
最后,我将打印图案大小的时间:


让我们首先定义一个方法来构造这两种类型的线

def make_line(n, mid_char)
  ['*', *[mid_char]*(n-2), '*'].join(' ')
end

make_line(5, '*') #=> "* * * * *" 
make_line(5, '#') #=> "* # # # *" 
现在创建一个方法,以所需的模式绘制线。只有当
(i+1)%3
等于零时,
i
第四行(基零)才由星星和空格组成;除此之外,该行还包含英镑符号

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


矩形(3)
看起来怎么样?你知道如何打印字符吗?你知道怎么写循环吗?您面临的具体问题是什么?谢谢大家,已经解决了。只需对我使用模运算:DYour代码实际上打印的是
#
,而不是
=
,并且缺少空格。将返回什么
square('blablabla')
)@SPN,现在在每第三行结束时有额外的空白。考虑使用保护子句进行参数检查。类似于
raisetypeerror的东西,除非n是a?整数
作为方法的第一行。
平方(1)
返回
必须是整数打印“*”
将在每行末尾产生一个多余的空格。
除非count.is_是?(整数)和&count.odd?
可能更容易阅读。不必覆盖数组中的
nil
值,您可以将一个块传递给
array.new
来直接指定元素。Cycle+take,我喜欢。这里的所有答案都很好,但这个答案简单而优雅。我把这个答案标记为我问题的答案谢谢!
pattern = [a, a, b].cycle
puts pattern.take(size)
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
* = = = = = *
* * * * * * *
* = = = = = *
def make_line(n, mid_char)
  ['*', *[mid_char]*(n-2), '*'].join(' ')
end

make_line(5, '*') #=> "* * * * *" 
make_line(5, '#') #=> "* # # # *" 
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
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *
* # # # # # *
* * * * * * *
* # # # # # *