Ruby - 如果Elsif Else错误

Kel*_*yle 5 ruby if-statement

我在这里遇到一个简单的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
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:11:语法错误,意外的keyword_elsif elsif array.at(i)=='e'^

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:13:语法错误,意外的keyword_elsif elsif array.at(i)=='i'^

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:15:语法错误,意外的keyword_elsif elsif array.at(i)=='o'^

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:17:语法错误,意外的keyword_elsif elsif array.at(i)=='u'^

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:19:语法错误,意外的keyword_else

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:21:语法错误,意外的keyword_end

C:/ Users/Kelan/Documents/Programming/Ruby Files/Little Programs/Alphabet.rb:25:语法错误,意外$ end,期待keyword_end puts'辅音:'+ cons.to_s ^

[完成0.203秒]

我确定这只是一些愚蠢的事情,但我一直在网上寻求帮助,我听说过你的社区,所以我想我会在这里尝试,

珂兰

mar*_*aro 15

Ruby中没有++运算符.您应该使用过+= 1 您可能还想了解case声明:

alphabet = 'abcdefghijklmnopqrstuvwxyz'

26.times do |i|
    case alphabet[i]
        when 'a' then vowel += 1
        when 'e' then vowel += 1
        when 'i' then vowel += 1
        when 'o' then vowel += 1
        when 'u' then vowel += 1
        else cons += 1
    end#end case
end#end times

puts 'Vowel: ' + vowel.to_s
puts 'Consonants: ' + cons.to_s
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,使用count类中的方法String,如下所示:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
vowels = 'aeiou'
vowel_count = alphabet.count vowels
cons_count = alphabet.length - vowel_count
puts "Vowels: #{vowel_count}"
puts "Consonants: #{cons_count}"
Run Code Online (Sandbox Code Playgroud)

  • 你还可以重载案例:'当'a','e','i','o','u'然后是元音+ = 1 (3认同)

cor*_*ard 5

您的问题是您正在使用Java/PHP/C样式增量运算符.Ruby并没有失败.你必须foo += 1改用.

我怎么样向你展示一种更加Ruby的方式呢?

# use a range to define your alphabet
alphabet = ('a'..'z').entries  #=> ['a', 'b', 'c', ...]

# define vowels as members of an array. it's more flexible, which
# is great for things that change (what if you decide to use 'y'?)
vowels = %w{ a e i o u }  #=> ['a', 'e', 'i', 'o', 'u']

# keep counts all together in a hash, which I personally find cleaner
counts = { :vowels => 0, :consonants => 0 }

# even the `for` loops in ruby use the iterators, so you actually
# get better performance out of using the more user-friendly `.each`
alphabet.each do |letter|
  if vowels.include? letter
    counts[:vowels] += 1
  else 
    counts[:consonants] += 1
  end
end

puts "There were #{counts[:vowels]} vowels and #{counts[:consonants]} consonants."
Run Code Online (Sandbox Code Playgroud)