【发布时间】:2016-08-11 23:24:11
【问题描述】:
我想创建一个生成 2 个随机数 (num1,num2)(比如说从 1 到 100)的小方法,但是这些数字需要一起整除
用更多的数学术语,我想生成 2 个随机数,其中商的余数为 0
我这样做了:
def operation
operators = [:/, :+, :-, :*]
@operation = operators.sample
end
def result(num1, num2)
if @operation == :/
unless num1 % num2 != 0
numbers
else
@result = @num1.send(@operation, @num2)
end
else
@result = @num1.send(@operation, @num2)
end
end
def numbers
@num1 = rand(1..100)
@num2 = rand(1..100)
end
numbers
result(@num1,@num2)
这个想法是,除非两个数字的其余部分是 0,否则它会再次返回 number 方法。我相信这里有一些迭代问题,因为我收到一个错误'`result':nil is not a symbol nor a string (TypeError)'
谢谢
【问题讨论】:
标签: ruby operators symbols division