Ruby——如果 Elsif Else 错误

发布于 2024-11-06 00:17:52 字数 1751 浏览 4 评论 0原文

我在这里遇到一个简单的 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_else

C:/Users/Kelan/Documents/Programming/Ruby
Files/Little Programs/Alphabet.rb:21:
syntax error, unexpected keyword_end

C:/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

后知后觉 2024-11-13 00:17:52

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

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

alphabet = 'abcdefghijklmnopqrstuvwxyz'
vowels = 'aeiou'
vowel_count = alphabet.count vowels
cons_count = alphabet.length - vowel_count
puts "Vowels: #{vowel_count}"
puts "Consonants: #{cons_count}"

There is no ++ operator in Ruby. You should have used += 1
You may also want to learn about case statement:

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

Or, even better, use method count from class String, like this:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
vowels = 'aeiou'
vowel_count = alphabet.count vowels
cons_count = alphabet.length - vowel_count
puts "Vowels: #{vowel_count}"
puts "Consonants: #{cons_count}"
强辩 2024-11-13 00:17:52

您的问题是您正在使用 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."

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?

# 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."
我爱人 2024-11-13 00:17:52

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

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

I think rather than vowel++ and con++, you need to use vowel+=1 and con+=1.

Ruby does not have C-style pre/post incrementors.

鱼窥荷 2024-11-13 00:17:52

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

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

Here is yet another way to write the demo:

puts("%d vowels & %d consonants" % ('a'..'z').inject([0,0]) do |m, e|
    m[/[aeiou]/.match(e) ? 0:1] += 1; m 
  end)
生生漫 2024-11-13 00:17:52
  • 有一种简单的方法可以使用 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}"
  • There is an easy way of constructing the set of alphabets, using Range.
  • Since you are using ruby, you should use internal iterators instead of the external ones. You will rarely see a 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.
  • There is no ++ or -- operators in ruby. Use += 1.
  • Use "#{ }" notation. It's much better and faster than using +. In fact, you can omit to_s if you use it.

I would go like this:

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}"

If I wanted a shorter one, I might go with this:

partition = ('a'..'z').group_by{|c| c =~ /[aeiou]/}
puts "Vowel: #{partition[0].length}"
puts "Consonants: #{partition[nil].length}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文