2011-11-01 37 views
0

我正在做邊緣koans學習紅寶石,我陷入了貪婪koan(182-183)得到一個神祕的錯誤。規則概述HERERuby koan 182貪婪骰子游戲 - 得到一個神祕的錯誤

我知道我的代碼是..不起眼的,我想我會重構它,一旦我的邏輯是健全的(它可能不是)。

我很感激任何幫助。

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...] 

回答

0

問題不在於與elif雖然這樣的,但與之前的令牌錯誤消息的聲音:

count[0]++ 

是Ruby中沒有允許的語法。

5

Ruby不支持C風格增量:++

使用count[0] += 1

如果你有某種「神祕」的錯誤,你也應該看不到哪解釋指出你,還一個線之上。

0

Ruby沒有後增加運算符(即++);你應該使用+=操作:

a = 5 
puts a # prints 5 
a += 1 
puts a # prints 6 

該錯誤消息是有點分心,但如果你換了所有的++運營商與+= 1,該代碼應該工作正常。

查看更詳細的討論here