【问题标题】:Ruby koan 182 Greed dice game - getting a mysterious errorRuby koan 182 贪婪骰子游戏 - 出现神秘错误
【发布时间】:2011-12-19 17:14:36
【问题描述】:

我正在做边缘案例 koan 来学习 ruby​​,但我遇到了 greed koan (182-183) 的一个神秘错误。规则概述HERE

我知道我的代码是..不起眼的,我想一旦我的逻辑合理(可能不是)我会重构它。

感谢您的帮助。

def score(dice)
  score = 0
  if dice == []
    return score
  end

  dice = dice.sort
  dice = [1,1,4,5,6]
  count = [0,0,0,0,0,0]
  score = 0
  dice.each do |face|
    if    face == 1
        count[0]++
    elsif face == 2 # this is line 45 with reported error
        count[1]++
    elsif face == 3
        count[2]++
    elsif face == 4
        count[3]++
    elsif face == 5
        count[4]++
    elsif face == 6
        count[5]++
    end
  end
  if count[0] >= 3
    score+= 1000
    count[0] = count[0] - 3
  elsif count[4] >= 3
    score+= 500
    count[4] = count[4] - 3
  end
  score+= count[0] * 100
  count [0] = 0
  score+= count[4] * 50
  count [4] = 0

  if count[1] >= 3
    score+= 200
  elsif count[2] >= 3
    score+= 300
  elsif count[3] >= 3
    score+= 400
  elsif count[5] >= 3
    score+= 600
  end

  #check if there are three 1 at the beginning
  #if not, check if we have three 2
  # You need to write this method
end

有关信息,我收到此错误:

 /Users/gozulin/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': /Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:45: syntax error, unexpected keyword_elsif (SyntaxError)
    elsif face == 2
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:47: syntax error, unexpected keyword_elsif
    elsif face == 3
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:49: syntax error, unexpected keyword_elsif
    elsif face == 4
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:51: syntax error, unexpected keyword_elsif
    elsif face == 5
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:53: syntax error, unexpected keyword_elsif
    elsif face == 6
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:55: syntax error, unexpected keyword_end
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:84: class definition in method body
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:122: syntax error, unexpected $end, expecting keyword_end
    from /Users/gozulin/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from path_to_enlightenment.rb:24:in `<main>'
rake aborted!
Command failed with status (1): [/Users/gozulin/.rvm/rubies/ruby-1.9.2-p290...]

【问题讨论】:

    标签: ruby


    【解决方案1】:

    Ruby 不支持 C 风格的增量:++

    使用count[0] += 1

    如果你有某种“神秘”的错误,你也不应该看解释器指向你的地方,而应该看上面的一行。

    【讨论】:

      【解决方案2】:

      问题不在于elif,尽管错误消息听起来像这样,但之前的令牌:

      count[0]++
      

      在 ruby​​ 中是不允许的语法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-05
        • 2012-02-29
        • 2021-12-14
        • 2015-08-25
        • 2015-12-24
        • 1970-01-01
        相关资源
        最近更新 更多