2012-09-06 71 views
-1

當試圖編寫一個二次方程的代碼來計算出x時,我得到一個錯誤,說明括號存在問題 這裏是代碼我已經進入了這個計算器quad.rb:8:syntax error,unexpected tIDENTIFIER,expected')'

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 

任何幫助的時候?非常感謝你

+0

添加四個空格代碼行前做出是否使用的是計算器他們正確地格式化 –

+0

?例如,TI-Basic與卡西歐和惠普的差別很大。一些現代計算器(如TI nspire)允許您在符號之間省略乘法運算符,但其他運算符則需要它,所以我們需要更多信息。 – Dai

+0

請你現在更新你的問題的格式,你有幾次被要求這樣做,並已被告知如何。 –

回答

0

你很多地方失蹤*。例如。 4 *一* C代替4AC

+0

謝謝託尼的迴應,我改變了這些錯誤,然後我留下了這個錯誤 quad.rb:8:語法錯誤,意外的tIDENTIFIER,預計$結束 – user1650578

+0

您需要修復您的代碼的格式問題...很難讀 –

+0

我對此很新,你知道我需要格式嗎? – user1650578

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}" 
+0

如果你解釋你如何修改代碼,以及爲什麼,這將是一個更好的答案。另外請注意,這個問題已經有一年多了。 –

相關問題