Ruby——如果 Elsif Else 错误
我在这里遇到一个简单的 if else 链的错误,我不知道发生了什么。前几天我开始学习 ruby,我已经了解了一些 java,并且只是尝试重新编写程序以更快地学习 ruby。我正在尝试计算元音和辅音。无论如何,这是我的代码...
#!/usr/bin/ruby/
alphabet = 'abcdefghijklmnopqrstuvwxyz'
array = alphabet.chars.to_a
vowel = 0
cons = 0
puts array.at(1)
for i in 0...26
if array.at(i) == "a"
vowel++
elsif array.at(i) == 'e'
vowel++
elsif array.at(i) == 'i'
vowel++
elsif array.at(i) == 'o'
vowel++
elsif array.at(i) == 'u'
vowel++
else
cons++
end#end if else chain
end#end for loop
puts 'Vowel: ' + vowel.to_s
puts 'Consonants: ' + cons.to_s
这是我收到的错误:
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:11: 语法错误,意外的 keywords_elsif elsif array.at(i) == 'e' ^
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:13: 语法错误,意外的 keywords_elsif elsif array.at(i) == 'i' ^
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:15: 语法错误,意外的 keywords_elsif elsif array.at(i) == 'o' ^
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:17: 语法错误,意外的 keywords_elsif elsif array.at(i) == 'u' ^
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:19: 语法错误,意外的keyword_else
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:21: 语法错误,意外的keyword_end
C:/Users/Kelan/Documents/Programming/Ruby 文件/小程序/Alphabet.rb:25: 语法错误,意外的 $end, 期待关键字_end 输入 '辅音:' + cons.to_s ^
[0.203秒内完成]
我确信这只是一些愚蠢的事情,但我一直在网上寻求帮助,而且我听说过你们伟大的社区,所以我想我会在这里尝试,
Kelan
I'm getting an error here with a simple if else chain, and I can't figure out what is going on. I started learning ruby the other day, I already know some java, and was just trying to re-write programs to learn ruby faster. I am trying to tally vowels and consonants. Anyways here is my code...
#!/usr/bin/ruby/
alphabet = 'abcdefghijklmnopqrstuvwxyz'
array = alphabet.chars.to_a
vowel = 0
cons = 0
puts array.at(1)
for i in 0...26
if array.at(i) == "a"
vowel++
elsif array.at(i) == 'e'
vowel++
elsif array.at(i) == 'i'
vowel++
elsif array.at(i) == 'o'
vowel++
elsif array.at(i) == 'u'
vowel++
else
cons++
end#end if else chain
end#end for loop
puts 'Vowel: ' + vowel.to_s
puts 'Consonants: ' + cons.to_s
Here is the error I am getting:
C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:11:
syntax error, unexpected keyword_elsif
elsif array.at(i) == 'e'
^C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:13:
syntax error, unexpected keyword_elsif
elsif array.at(i) == 'i'
^C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:15:
syntax error, unexpected keyword_elsif
elsif array.at(i) == 'o'
^C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:17:
syntax error, unexpected keyword_elsif
elsif array.at(i) == 'u'
^C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:19:
syntax error, unexpected keyword_elseC:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:21:
syntax error, unexpected keyword_endC:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:25:
syntax error, unexpected $end,
expecting keyword_end puts
'Consonants: ' + cons.to_s
^[Finished in 0.203 seconds]
I'm sure it's just something silly, but I've been looking forever online for help and I have heard of your great community, so I thought I would try here,
Kelan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Ruby 中没有 ++ 运算符。您应该使用
+= 1
您可能还想了解
case
语句:或者,更好的是,使用
String
类中的方法count
,如下所示:There is no ++ operator in Ruby. You should have used
+= 1
You may also want to learn about
case
statement:Or, even better, use method
count
from classString
, like this:您的问题是您正在使用 Java/PHP/C 风格的增量运算符。 Ruby 并没有因此而沮丧。您必须使用 foo += 1 来代替。
我向您展示一种更 Ruby 的方式来执行此操作怎么样?
Your problem is you're using the Java/PHP/C style increment operator. Ruby isn't down with that. You have to use
foo += 1
instead.How about I show you a more Ruby way of doing this though?
我认为您需要使用
vowel+=1
和con+=1
,而不是vowel++
和con++
。Ruby 没有 C 风格的前置/后置增量器。
I think rather than
vowel++
andcon++
, you need to usevowel+=1
andcon+=1
.Ruby does not have C-style pre/post incrementors.
这是编写演示的另一种方法:
Here is yet another way to write the demo:
Range
构建字母集。for
循环。case
构造非常方便。您可以将多个匹配模式放入一个,并用逗号分隔。++
或--
运算符。使用+= 1
。"#{ }"
表示法。它比使用+
更好、更快。事实上,如果使用的话,可以省略to_s
。我会这样:
如果我想要一个较短的,我可能会这样做:
Range
.for
loop in a good program in ruby.case
construction is handy in this case. You can put multiple matching patterns into one, separated by comma.++
or--
operators in ruby. Use+= 1
."#{ }"
notation. It's much better and faster than using+
. In fact, you can omitto_s
if you use it.I would go like this:
If I wanted a shorter one, I might go with this: