我是Ruby编程的初学者。我的程序是给定字符串中单词长度的计数。但它显示了以下错误对于[0,0]未定义的方法'<':Array <NoMethodError>
未定义的方法 '<' 为[0,0]:数组
这里是我的代码
def even(words, n)
i = 0, m = 0
while i < n do
count = count + words[i].length
if count%2 == 0 then
m = m + 1
end
i = i + 1
end
return m
end
prinnt "Enter The String:"
s = gets.chomp
words = s.split()
n = words.length
x = even(words, n)
puts x
用'words = s.split(“”)替换'words = s.split()',否则你的输入不会被字符分割 – krichard 2012-07-16 07:36:17