gpt4 book ai didi

Ruby: "Unexpected keyword_end"...但所有的开场白和闭幕式都匹配

转载 作者:行者123 更新时间:2023-12-03 08:18:25 25 4
gpt4 key购买 nike

我正在编写一段代码,它将向用户返回第 n 个素数。第 19 行和第 22 行出现“unexpected keyword_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)

我查看了很多关于“unexpected keyword_end”错误的其他 Stack Overflow 问题,但所有这些问题都是由于作者的代码中有太多“end”而引起的。我相信,经过多次检查,我的代码中有正确数量的“结束”关闭器

还有什么问题?

最佳答案

i ++i += 1number ++number += 1 . Ruby 不支持 ++--运营商。阅读这个问题 No increment operator (++) in Ruby?并阅读Why doesn't Ruby support i++ or i— (increment/decrement operators)?

关于Ruby: "Unexpected keyword_end"...但所有的开场白和闭幕式都匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20915012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com