-2
def convert(temp)
new_temp = (temp - 32) * 5/9
return new_temp
end
fahrenheit_temp = gets.to_i
celsius_temp = convert(fahrenheit_temp)
puts celsius_temp
提出:
C:/Users/Asd/Documents/Aptana Studio 3 Workspace/test/test.rb:3:in `convert': undefined local variable or method `new_temp' for main:Object (NameError)
from C:/Users/Asd/Documents/Aptana Studio 3 Workspace/test/test.rb:7:in `<main>
有什麼幫助嗎?我不明白爲什麼return
方法會導致此問題。
我不能重複的問題。請閱讀「[mcve]」。將snake_case用於方法和變量名稱,notCamelCaseBecauseItIsHarderToRead。 Ruby會返回最後一個值,因此'return'是多餘的。結果可以將該方法簡化爲「(temp-32)* 5/9」。 –
編輯的問題。感謝您的建議 –
如果你運行這個具體的片段,它會工作。 – yzalavin