希望的東西newbish不到我最後...紅寶石:「在'<=':與長整數數組的比較失敗(引發ArgumentError)」
總之,我做了一下上一個小應用程序編碼的那將數字簡化爲素數,主要用於幫助小作業,如家庭作業。
然而,一個特定的方法是給我在標題中提到的錯誤:
def get_simps(num)
curr = 2
print("Working...")
while (curr <= num)
#If they divide cleanly, then it's a simplified form
if (num % curr == 0)
res = [curr, num/curr]
break
end
curr += 1
end
print("\n")
return res
end
凡說法NUM此語句不提供:
print("Insert number here: ")
num = gets().chomp().to_i()
因此使得錯誤怪異:爲什麼說我比較了Fixnum和ARRAY?我也是這樣做的:
if (num.class() == curr.class())
print "Cheese"
end
它打印奶酪。爲什麼錯誤的原因呢?
Copypasting到IRB與'get_simps調用(gets()函數。的Chomp()。to_i()) '不會給我一個錯誤(雖然它確實給出了錯誤的結果:get_simps(15000)=> [2,7500] – Tordek 2009-12-13 00:32:50
你發佈的代碼對我來說工作正常。你是否在做'get_simps(get_simps(20 )'' – 2009-12-13 00:33:28
此外,「錯誤」,因爲我期待它返回一個素數分解的數字 – Tordek 2009-12-13 00:35:13