Ruby 用99瓶啤酒绕线时出现问题
我能倒数到97瓶啤酒,但我在倒数到1时遇到了麻烦。可以用我写的东西创建一个循环吗?这是我到目前为止所拥有的Ruby 用99瓶啤酒绕线时出现问题,ruby,Ruby,我能倒数到97瓶啤酒,但我在倒数到1时遇到了麻烦。可以用我写的东西创建一个循环吗?这是我到目前为止所拥有的 all_beers = (99).to_s one_less = ((all_beers).to_i - 1).to_s puts '' + all_beers + ' bottles of beer on the wall, ' + all_beers + ' bottles of beer. You take one down you pass it around ' +
all_beers = (99).to_s
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'
all_beers = one_less
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'
是的,这当然是可能的。这是取自:
#
#Rubeer.rb
#Eric Budd,2008年1月
#
#演示如何向内置类、可选方法参数和内联添加功能
#条件句、字符串替换、酒精厌恶和过度花哨地使用哈希。
#
#这借用了Daniel Straight对“wordalize”方法的出色实现中的哈希
#
类整数
数字单词={0=>“不”,1=>“一”,2=>“二”,3=>“三”,4=>“四”,5=>“五”,
6=>“六”,7=>“七”,8=>“八”,9=>“九”,
10=>“十”,11=>“十一”,12=>“十二”,13=>“十三”,
14=>“十四”,15=>“十五”,16=>“十六”,17=>“十七”,
18=>“十八”,19=>“十九”,
20=>“二十”,30=>“三十”,40=>“四十”,50=>“五十”,60=>“六十”,
70=>“七十”,80=>“八十”,90=>“九十”}
定义文字化
raise“无效数字到文字化-应在(0..99)范围内”,除非(0..99)==self
如果self<20,则返回数字\字[self]
wordalized=字数[self-(self%10)]
文字化+='-'+数字字[self%10],除非(self%10)=0
返回文字化
结束
def瓶
提高“无效瓶数-应在(0..99)范围内”,除非(0..99)==self
多少瓶=self.wordalize+“瓶”
除非self==1,否则有多少瓶+='s'
归还多少瓶
结束
别名:瓶子:为纳粹分子准备的瓶子
结束
def sing(数字,总计=false)
饮料=戒酒者?“可乐:'啤酒'
把{饮料}的{数字.瓶子.大写}放在墙上,{饮料}的{数字.瓶子}放在墙上
如果是数字!=0
在墙上写上“拿下一瓶,传来传去,{(数字-1)。几瓶}{饮料}。\n\n”
其他的
在墙上写着“去商店再买99瓶饮料。”
结束
结束
99.下至(0){|数字|星(数字)}
#取消注释替代Teetotherler版本的以下内容
#99.downto(0){| number | sing(number,true)}
这里上传了多个Ruby版本,但是默认版本对于初学者来说太复杂了。不过,这一个非常好,你应该能够理解发生了什么
回答你问题的要点是99.downto(0){number}
。这是一个循环,它将重复大括号内的任何内容(在本例中,sing(number)
)一百次,number
从99
到0
还要注意的是,将数字作为字符串((99).to_s
)携带并在需要时将其转换回整数是低效的(而且难以辨认);更确切地说,当您显示它时,让它始终是一个整数,并在您需要它作为字符串之前将其转换为字符串(或者让字符串串联/插值自动为您执行,如在这段代码中)
虽然Ruby确实有for
和While
循环,但它们很少(While
)或从未(for
)使用。相反,rubyst通常依赖于迭代器和枚举器。其他函数,如Integer#downto
是Integer#upto
,Integer#次
,以及几乎所有最棒的混音功能。使用:
它将从您想要的号码循环到您想要的号码
99.downto(1).each do |s|
all_beers = s
one_less = s - 1
puts '' +
all_beers.to_s + ' bottles of beer on the wall, ' +
all_beers.to_s + ' bottles of beer. You take one down you pass it around ' +
one_less.to_s + ', beers on the wall!'
end
简单的回答是肯定的,你可以用你写的东西做一个循环,使用ruby做循环有很多方法。然而,由于这似乎是关于学习编程的,考虑到您没有使用任何控制结构或字符串插值,更不用说没有多大意义的强制转换,我建议在使用ruby时学习编程的概念 除此之外,您还可以执行以下操作:
(1..99).reverse_each do |number|
bottle = number == 1 ? 'bottle' : 'bottles'
verse = "#{number} #{bottle} of beer on the wall, #{number} #{bottle} of beer. "
verse << "Take one down, pass it around, #{number-1} #{number-1 == 1 ? 'bottle' : 'bottles'} of beer on the wall"
puts verse
end
(1..99)。反转每个do的编号|
瓶子=数量==1?“瓶子“:“瓶子”
verse=“#{号码}{瓶}的啤酒挂在墙上,#{号码}{瓶}的啤酒。”
你可以用来让你的生活更轻松的东西:,如果(--that--thing-->?:)
我喜欢第一次体验这个练习,所以我有点犹豫是否在这里提供一个答案,但事情就是这样
它稍微高级一点,但是使用“proc”来确保您正确地将“瓶子”多重化是一种很好且干净的方法
“downto()”也是一种非常棒的方式,可以反复浏览这99个瓶子,因为它让你感觉你在读英语而不是代码
num_at_start=99#您可以更改此号码。
num|u瓶=proc{n}“{n}瓶{n==1?”“'s'}”
num|u在|u开始时。向下至(1)do|num|
在墙上打印“{num_瓶。调用(num)}瓶啤酒,”+
“{num_瓶。调用(num)}瓶啤酒!\n”+
“你拿下一个,传来传去”
除非num==1
把“{num_瓶。把(num-1)}瓶啤酒挂在墙上!”
其他的
“墙上不再有啤酒了!”
结束
结束
资料来源:Chris Pine编写的《学习编程第二版》(虽然我做了一些修改)这是我为这个问题找到的修复方法:
beer = 99
while beer > 0
puts beer.to_s + " bottles of beer on the wall. " + beer.to_s +
" bottles of beer."
(beer -= 1).to_s
puts "Take one down, pass it around. " + beer.to_s +
" bottles of beer on the wall."
end
你是说像使用printf?可以使用以下字段类型字符格式化printf:
beer = 99
while beer > 0
puts beer.to_s + " bottles of beer on the wall. " + beer.to_s +
" bottles of beer."
(beer -= 1).to_s
puts "Take one down, pass it around. " + beer.to_s +
" bottles of beer on the wall."
end