繁体   English   中英

出乎意料的以看似正确的 ruby​​ 代码结尾

[英]Unexpected end on a ruby code that seems right

我正走在 ruby​​ koans 启蒙之路上,但一个无形的障碍出现在我面前。 在about_scoring_project我receveing在哪里我已经把线一些语法错误end以前for

C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
: C:/Users/Psico/Downloads/rubykoans/koans/about_scoring_project.rb:62: syntax error, unexpected `end' (SyntaxError)
  end
  ^~~
C:/Users/Psico/Downloads/rubykoans/koans/about_scoring_project.rb:74: syntax error, unexpected `end'
  end

我试图删除这些结尾,但随后我收到了一个预期的“结尾”错误。

我的代码如下:

class DiceError < StandardError
end


def score(dice)
  # You need to write this method
  totalScore = 0
  numbers = {1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0}
  for elem in dice
    unless (elem >= 1 && elem <= 6)
      raise DiceError, "#{elem} is not a number of a six faced dice"
    end

    numbers[elem] += 1

  end

  valuesArray = numbers.values
  indexCounter = 0

  for elem in valuesArray   ## conta pontos de triplas #TODO passar para uma função separada 
    if elem >= 3
      if indexCounter == 0
        totalScore += 1000
      else
        totalScore += 100 * (indexCounter + 1)
      end
      valuesArray[indexCounter] = elem - 3
    end
    indexCounter++
  end

  indexCounter = 0 #zera o contador
  singlesPoints = Hash.new(0)
  singlesPoints[0] = 100
  singlesPoints[4] = 50
  for elem in valuesArray
    if elem > 0
      totalScore += singlesPoints[indexCounter] * elem
    end
    valuesArray[indexCounter] = 0 #garante que no fim o vetor de valores seja todo igual a 0
    indexCounter++
  end

  puts("#{numbers}") 
  return totalScore
end

您在class DiceError < StandardError下的代码的第二行有一个end

当您开始编写类时,这可能会自动放在那里。 尝试将其移至最后一行(在end之后):

  puts("#{numbers}") 
    return totalScore
  end
end

希望这可以帮助 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM