用户输入三个条目:
鉴于用户输入,我要生成一个文本,如下所示。鉴于:
"A"
"B"
"3"
结果应该是:
"AB AB AB"
答案 0 :(得分:2)
print 'Name: '
name = gets.chomp
print 'Nickname: '
nickname = gets.chomp
print 'Number: '
num = gets.to_i
result = ("#{name}#{nickname} " * num).strip
puts "Result is: #{result}"
名称:A
昵称:B
编号:3
结果是:AB AB AB
如果您不关心尾随空格而只想打印结果,则可以通过删除strip()来简化代码:
print 'Name: '
name = gets.chomp
print 'Nickname: '
nickname = gets.chomp
print 'Number: '
num = gets.to_i
result = "#{name}#{nickname} " * num
puts "Result is: #{result}"
答案 1 :(得分:1)
name = 'A'
nickname = 'B'
rep = 3
(name + nickname) * rep # produces "ABABAB"
Array.new(rep, name + nickname).join(' ') # produces "AB AB AB"
答案 2 :(得分:1)
尝试以下操作:
name = 'A'
nick = 'B'
rep = 3
arr = []
rep.times do |i|
arr.push("#{name}#{nick}")
end
p arr # => ["AB", "AB", "AB"]
说明:
times
:迭代给定的块int时间,从doc 答案 3 :(得分:0)
update fact_training_event_tbl
join fact_training_event_tbl base
on base.gkey = fact_training_event_tbl.gkey + 1
set course_date = base.course_date