我是一般的Ruby和編程新手。 我一直在試圖把一個隨機創建數學問題的小腳本放在一起,但是我在生成一個隨機數學運算符時遇到了麻煩。如何評估隨機數學運算符?
我至今腳本:
num = (1..10).to_a
num1 = num.shuffle[0]
num2 = num.shuffle[0]
op = %w{+ - = /}
op1 = op.sample
puts w = "#{num1} #{op1} #{num2}"
puts "Your answer is:"
answer = gets()
solution = num1 + "what do I put here??" + num2
if answer.to_i == solution
puts "Correct! The answer is #{solution}"
else
puts "Incorrect, the answer is #{solution}"
end
我已經成功地挑選出一個隨機的運營商,但因爲它是一個字符串,不能在解決方案中使用它。
哦,你可能會改變標題 - 你解決了隨機算子問題,你的問題實際上是如何評估字符串。 – 2012-02-15 17:52:03