Ruby - 如果Elsif Else错误

时间:2011-05-11 00:08:18

标签: 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

以下是我遇到的错误:

  

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:11:   语法错误,意外的keyword_elsif     elsif array.at(i)=='e'          ^

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:13:   语法错误,意外的keyword_elsif       elsif array.at(i)=='i'            ^

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:15:   语法错误,意外的keyword_elsif       elsif array.at(i)=='o'            ^

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:17:   语法错误,意外的keyword_elsif       elsif array.at(i)=='你'            ^

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:19:   语法错误,意外的keyword_else

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:21:   语法错误,意外的keyword_end

     

C:/用户/克兰/文档/编程/红宝石   文件/小程序/ Alphabet.rb:25:   语法错误,意外$ end,   期待keyword_end投放   '辅音:'+ cons.to_s                                  ^

     

[完成0.203秒]

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

克兰

5 个答案:

答案 0 :(得分: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

或者,更好的是,使用类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}"

答案 1 :(得分: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."

答案 2 :(得分:3)

我认为,而不是vowel++con++,您需要使用vowel+=1con+=1

Ruby没有C风格的前/后增量器。

答案 3 :(得分:2)

这是编写演示的另一种方法:

puts("%d vowels & %d consonants" % ('a'..'z').inject([0,0]) do |m, e|
    m[/[aeiou]/.match(e) ? 0:1] += 1; m 
  end)

答案 4 :(得分:2)

  • 使用Range可以轻松构建字母集。
  • 由于您使用的是ruby,因此应使用内部迭代器而不是外部迭代器。你很少会在ruby的好程序中看到for循环。
  • 在这种情况下,
  • case构造很方便。您可以将多个匹配模式放在一个中,用逗号分隔。
  • ruby​​中没有++--个运算符。使用+= 1
  • 使用"#{ }"表示法。它比使用+更好更快。实际上,如果您使用它,可以省略to_s

我会这样:

vowel = 0
cons = 0
('a'..'z').each do |c|
  case c
  when 'a', 'e', 'i', 'o', 'u'; vowel += 1
  else                          cons += 1
  end
end

puts "Vowel: #{vowel}"
puts "Consonants: #{cons}"

如果我想要一个较短的,我可能会这样:

partition = ('a'..'z').group_by{|c| c =~ /[aeiou]/}
puts "Vowel: #{partition[0].length}"
puts "Consonants: #{partition[nil].length}"
相关问题