我正在编写一段代码,它将向用户返回第 n 个素数。我在第 19 行和第 22 行收到“unexpected keywords_end”语法错误。我在代码中添加了注释,以便您可以轻松找到错误的位置。
def nthPrime(n)
number = 3
primeNumber = 1
i = 0
primes = [2]
#Iterates the number until we've found the desired number of primes.
while primeNumber < n
#Iterates through the prime numbers already found to locate prime factors.
while i < primes.length
#Returns TRUE if a prime factor is found.
#If no prime factors are found, primeNumber ticks up by one, and the number
#is added to the list of primes.
if number % primes[i] != 0
if i == primes.length
primes << number
i = 0
else
i ++
end #Unexpected keyword_end
end
number ++
end #Unexpected keyword_end
end
puts number
end
nthPrime(6)
我查看了许多其他有关“意外的keyword_end”错误的堆栈溢出问题,但所有这些问题都是由于作者在代码中包含太多“end”而引起的。我相信,经过多次检查,我的代码中有正确数量的“结束”关闭器
还有什么问题?
Write i ++
as i += 1
and number ++
as number += 1
。红宝石不支持++
or --
运营商。阅读这个问题Ruby 中没有增量运算符 (++)? https://stackoverflow.com/questions/3717519/no-increment-operator-in-ruby并且还阅读了为什么 Ruby 不支持 i++ 或 i—(递增/递减运算符)? https://stackoverflow.com/questions/3660563/why-doesnt-ruby-support-i-or-i-increment-decrement-operators
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)