-1

当试图编写一个二次方程的代码来计算 x 时,我收到一个错误,说括号有问题 这是我为这个计算器输入的代码

puts "A"
a = gets.to_f
puts "B"
b = gets.to_f
puts "C"
c = gets.to_f

d = (-b+ ((b**2 - 4ac)**1/2))/2a
f = (-b- ((b**2 - 4ac)**1/2))/2a
puts d
puts f

我在尝试运行它时出现了这个错误

quad.rb:8: syntax error, unexpected tIDENTIFIER, expecting ')'
d = (-b+ ((b**2 - 4ac)**1/2))/2a
                     ^
quad.rb:8: syntax error, unexpected tIDENTIFIER, expecting $end

有什么帮助吗?非常感谢

4

2 回答 2

0

您在很多地方都缺少*。例如 4 * a * c 而不是 4ac

于 2012-09-06T01:23:47.050 回答
0
puts "I will solve a quadratic equation"
print "What is the value of a in ax^2+bx+c=0?"
a = gets.to_f;
print "What is the value of b in ax^2+bx+c=0?"
b = gets.to_f;
print "What is the value of c in ax^2+bx+c=0?"
c = gets.to_f;

d = b*b - 4*a*c
d = Math.sqrt(d)

e= -b/(2*a)

f= d/(2*a)

puts "The value of first root (x1) is #{e+f}"
puts "The value of second root (xe) is #{e-f}"
于 2014-04-01T19:36:15.280 回答