ruby - 语法错误,意外的 ',',期待关键字结束 arr.each_with_index |ele1, idx1|
问题描述
测试此方法时出现以下错误。
def opposite_count(nums)
pairs = []
arr.each_with_index |ele1, idx1|
arr.each_with_index |ele2, idx2|
if (idx2 > idx1) && (ele1 + ele2 == 0)
pairs << ele1
end
end
end
return pairs.length
end
目标是接收一组唯一数字并返回总和为 0 的元素对的数量。
出现的其他错误是:
syntax error, unexpected ',', expecting keyword_end
arr.each_with_index |ele2, idx2|
和
syntax error, unexpected keyword_end, expecting end-of-input
解决方案
你在两个循环do
之后都缺少一个关键字。each_with_index
此外,这会给你进一步的错误,因为arr
没有被定义。
推荐阅读
- c++ - 编码新手需要有关 if 语句的帮助
- imagemagick - 为什么imagemagick-convert只有jpeg作为委托时能够将jpg转换为png
- android - 房间数据库:即使它是自动生成的,仍然得到“没有为参数 'id' 传递值”
- amazon-web-services - AWS IAM ARN 和通配符
- python - 如何找到对列表进行排序的最小移动次数?
- c# - 将属于动态加载的 dll 的类强制转换为属于另一个 dll 的抽象类
- node.js - 如何在mongodb参考模型中进行记录
- javascript - 无法从 firebase 数据库中检索 OrderbyChild() 数据
- python - 从用户个人资料中自动删除 Facebook 帖子
- c# - 如何将原始类型列表绑定到 DataGrid