大写和downcase之间交替 [英] Alternate between uppercase and downcase
本文介绍了大写和downcase之间交替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想询问用户五次输入一个单词,并吐口水用大写和小写的每一行交替的答案,而无需使用 each_with_index
方法。我也想给答案按字母顺序排序。下面是我做了什么:
字= []5.times做
提出请输入一个字:
单词LT;< gets.chomp
结束 words.each做|字|
如果words.even?
把words.downcase
ELSIF words.odd?
把words.upcase
结束
结束
但一切我都试过失败。怎样才可以有大写和downcase之间的用户输入备用而不 each_with_index
方法?
解决方案
字=%W |怎么现在褐牛|枚举= [:upcase,:downcase] .cycle
words.map {| W | w.send enum.next}
#=> [HOW,现在,棕色,牛]
I'm trying to ask the user five times to enter a word, and spat the answers on each line alternating with uppercase and lowercase, without using the each_with_index
method. I'm also trying to sort the answers alphabetically. Below is what I have done:
words = []
5.times do
puts "Please enter a word:"
words << gets.chomp
end
words.each do |word|
if words.even?
puts words.downcase
elsif words.odd?
puts words.upcase
end
end
but everything I have tried has failed. How can I have the user input alternate between uppercase and downcase without the each_with_index
method?
解决方案
words = %w| how now brown cow |
enum = [:upcase, :downcase].cycle
words.map { |w| w.send enum.next }
#=> ["HOW", "now", " BROWN", "cow"]
这篇关于大写和downcase之间交替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文