这显示了一个错误,因为ruby范围规则阻止我访问if else块内的外部变量。如何绕过红宝石范围约定如果else语句
puts "Enter Line 1 m and c:"
m1 = gets.to_f
c1 = gets.to_f
puts "Enter Line 2 m and c:"
m2 = gets.to_f
c2 = gets.to_f
if ((m1==m2) and (c1==c2))
puts "infinite solutions"
elsif ((m1==m2) and (c1!=c2))
puts "no solution"
else
x = (c1 - c2)/(m2 - m1)
y = m1*x + c1
puts "(x,y) = (" + x + "," + y+")"
end
你能告诉我一种方法来解决这个错误吗?
更新:
实际上我得到的错误是: 未定义局部变量或者用于主要方法 'C1' :7 选自C;/Ruby200-X64 /斌/ IRB:从对象12;在''
你的条件对'elsif'是多余的。 'm1 == m2'就足够了。 – sawa 2015-03-03 14:30:54
你得到的范围错误是什么?我没有看到范围有什么问题。问题不明确。 – sawa 2015-03-03 14:34:29
我无法重现此错误。提到的一个错误是http://stackoverflow.com/a/28834277/2597260和http://stackoverflow.com/a/28834227/2597260在“puts”(x,y)=(“+ x +”, “+ y +”)“'。我已经尝试过在2.0.0(在家中)和网站上使用插值的代码:http://repl.it/cbg(2.2.0),它工作。 – 2015-03-03 15:30:27