有没有办法让这段代码更短更简单?
mapPartition()
答案 0 :(得分:1)
重构了一点:)
loop do
print "Please enter valid numbers (between 1 and 12): "
possibleSet = gets.chomp
break unless possibleSet.split(" ").map(&:to_i).any? {|e| (e<0 || e>12)}
end
答案 1 :(得分:1)
以下代码将检查输入的正确性:
input = loop do
print "Please enter valid numbers (between 1 and 12): "
# ⇓⇓⇓ as many spaces as user wants
input = gets.chomp.split(/\s+/).map(&:to_i) rescue []
break input unless input.empty? || input.any? { |i| !(0..12).include? i }
end
答案 2 :(得分:0)
这解析了数组中的用户输入(不完全相同的行为,但我希望它更干净,你可以从那里工作)
set = []
until set.all? {|i| (1..11).include?(i) } && !set.empty? do
set = gets.split(' ').map(&:to_i)
end