我是Ruby的新手,我尝试通过编写一个在温哥华找到徒步旅行点的简单终端程序来练习我的技能。 (虽然这可能不是最终编写程序的最佳方式,但我试图用这个程序测试我的数组技能。)
我所做的是在2维阵列中放置5个远足点。每个阵列包括徒步旅行的名称,位置,难度和长度。我希望用户输入他们的偏好,然后打印出匹配的远足。
E.g。如果用户输入6km,我想从< = 6km的阵列中打印出远足名称。
所以我想打印下面的代码,但只有当[0]< = 6 in"#{hikes [i] [0]}"
时 hikes.each_index{|i| puts "#{hikes[i][0]} " }
感谢您的帮助!
到目前为止,这是我的代码:
#Hiking Array: [Hike Name, Location, Difficulty, Length(km)]
hikes =
[["Admiralty Point", "Tri Cities", "Easy", 5],
["Habrich Ridge Trail", "Howe Sound", "Intermediate", 7],
["Aldergrove Regional Park", "Surrey", "Easy", 5],
["Alice Lake", "Howe Sound", "Easy", 6],
["Ancient Cedars Trail", "Whistler", "Intermediate", 5]]
puts "It's the weekend! Where should we go hiking?\n\n"
puts "Here is the list of available hikes."
puts "___________________________"
hikes.each_index{|i| puts "#{hikes[i][0]} " }
puts "\n"
puts "How many kilometers do we want to hike?"
kilometers = $stdin.gets.chomp
puts "\n"
puts "How hard do we want the hike to be, Easy or Intermediate?"
difficulty = $stdin.gets.chomp
puts "\n"
puts "What area of town do we want to hike in:"
puts "Tri Cities"
puts "Howe Sound"
puts "Surrey"
puts "Whistler"
area_of_town = $stdin.gets.chomp
puts "\n"
puts "Ok searching for awesome views that are #{kilometers}km, of #{difficulty} difficulty, and are in #{area_of_town}."
hikes.each_index{|i| puts "#{hikes[i][2]}"}
答案 0 :(得分:0)
也许
hikes.select {|arr| arr[3] <= 5}.each{|arr| puts arr[0]}
但我建议使用哈希,以便您可以使用名称来查找您正在寻找的属性。
hikes = [{'location'=>'Admirality', 'difficulty'=>5}, {'location'=>'Admirality', 'difficulty'=>10}, {'location'=>'Admirality', 'difficulty'=>15}]
hikes.select {|hsh| hsh["difficulty"] <= 10}
答案 1 :(得分:0)
只需使用“和”运算符&&
打印嵌套数组中的名称(如果它们符合所有3个条件,例如:
hikes.each do |h|
puts h[0] if ( h[3] <= kilometers &&
h[2].downcase == difficulty.downcase &&
h[1].downcase == area_of_town.downcase )
end
downcase
,以防万一用户输入与数组字符串不匹配(例如"Easy"
vs "easy"
)puts h[0]
更改为puts h.join(', ')
。 答案 2 :(得分:0)
您可以考虑按照以下方式(通过消除一些加息属性进行简化后):
hikes =
[["Admiralty Point", 5],
["Habrich Ridge Trail", 7],
["Aldergrove Regional Park", 5],
["Alice Lake", "Howe Sound", 6],
["Ancient Cedars Trail", 5]]
HIKES_BY_DISTANCE =
hikes.each_with_object(Hash.new {|h,k| h[k] = []}) {|a,h| h[a.last] << a}
#=> {5=>[["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5]],
# 7=>[["Habrich Ridge Trail", 7]],
# 6=>[["Alice Lake", "Howe Sound", 6]]}
def select_hikes(length_range)
HIKES_BY_DISTANCE.each_with_object([]) do |(k,v),a|
a.concat(v) if length_range.cover?(k)
end
end
select_hikes(4..5)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5]]
select_hikes(2..6)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(4..7)
#=> [["Admiralty Point", 5],
# ["Aldergrove Regional Park", 5],
# ["Ancient Cedars Trail", 5],
# ["Habrich Ridge Trail", 7],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(6..9)
#=> [["Habrich Ridge Trail", 7],
# ["Alice Lake", "Howe Sound", 6]]
select_hikes(7..7)
#=> [["Habrich Ridge Trail", 7]]
select_hikes(1..2)
#=> []
select_hikes(8..9)
#=> []