将输出排列在不同的行中

发布于 2024-12-09 16:16:14 字数 729 浏览 0 评论 0原文

只需要进行一些润色工作..

command = ""
names = []
while command != "exit"
puts 'please enter names seperated by a space: '

 command = gets.chomp!

 if command == "quit"
   names.sort! do|a,b| a.upcase <=> b.upcase end         # use {...} for do\end, in a single entry rather then multiple
    names.each_with_index do |name, index|
   if index % 2 == 0
       puts "#{name}", "\n"
      else
      puts "#{name.reverse}", "\n"
  end
 end
  else
    names << command
  end
end

因此该程序的作用是显示用户输入的许多名称。我不能让它做的就是在彼此下面显示名称。例如。 抢 账单 露西 劈 ... 然后向后显示每个第二个名字,并将它们显示在像这样的

rob

bill

lucy

Chop

======

llib

pohc 下。

lolmy 脑子熟了有什么建议吗?

just a bit of polish work needed..

command = ""
names = []
while command != "exit"
puts 'please enter names seperated by a space: '

 command = gets.chomp!

 if command == "quit"
   names.sort! do|a,b| a.upcase <=> b.upcase end         # use {...} for do\end, in a single entry rather then multiple
    names.each_with_index do |name, index|
   if index % 2 == 0
       puts "#{name}", "\n"
      else
      puts "#{name.reverse}", "\n"
  end
 end
  else
    names << command
  end
end

so what this progam does is display a number of names that where entered by the user. what i cant get it to do is display the names under each other. eg.
rob
bill
lucy
chop
...
its then to display every second name backwards and to display them under like this

rob

bill

lucy

chop

======

llib

pohc.

lolmy brain is cooked any suggestions??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-12-16 16:16:14

我会这样做:

names = []
while true
  puts 'please enter names seperated by a space: '
  command = gets.chomp
  break if command == 'exit' #exit loop if exit is entered
  command.split(' ').each {|a| names.push(a)} # split up each word with space as delimiter and insert into the array
end


temp = []
names.each do |each| #cycle thru array
  puts each # print each entry
  temp.push(each) if names.index(each).odd? #if entry is odd then add to temp array
end
puts '======'
temp.each {|each| puts each.reverse} #print each entry reversed

如果你愿意,我可以尝试修复你的......

Here is how I would do it :

names = []
while true
  puts 'please enter names seperated by a space: '
  command = gets.chomp
  break if command == 'exit' #exit loop if exit is entered
  command.split(' ').each {|a| names.push(a)} # split up each word with space as delimiter and insert into the array
end


temp = []
names.each do |each| #cycle thru array
  puts each # print each entry
  temp.push(each) if names.index(each).odd? #if entry is odd then add to temp array
end
puts '======'
temp.each {|each| puts each.reverse} #print each entry reversed

I can try to fix yours if you want...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文