[英]How to run calculation after methods are defined in Ruby?
試圖找到單個金額的未來價值。
我已經創建並定義了現值、比率和年數。 他們都一起工作,現在我試圖弄清楚如何獲取為每個輸入的數字並計算它們的最終值。
我使用的公式是:
(fv = pv * (1+r) ** n)
但我不確定如何讓 Ruby 運行計算?
這是我的程序代碼:
#單個金額的未來價值計算
def input
count = 0
puts "How much money you would like to invest? The amount must be at least $100 but no more than $50,000. (ex. 1000 = $1,000)"
pv = gets.chomp.to_i
while ((count < 3) && (pv < 100 || pv > 50000))
puts""
puts "YOU MUST ENTER A VALID NUMBER. PLEASE TRY AGAIN."
puts ""
count = count + 1
puts""
puts "How much money you would like to invest? The amount must be at least $100 but no more than $50,000. (ex. 1000 = $1,000)"
pv = gets.chomp.to_i
while ((count < 3) && (pv < 100 || pv > 50000))
puts""
puts "YOU MUST ENTER A VALID NUMBER. PLEASE TRY AGAIN."
count = count + 1
puts""
puts "How much money you would like to invest? The amount must be at least $100 but no more than $50,000. (ex. 1000 = $1,000)"
pv = gets.chomp.to_i
end
end
if (count >= 3)
puts ""
puts "TRY AGAIN LATER..."
puts "" && exit
return pv
end
count = 0
puts "What is the annual rate of interest? (ex. .08 = 8%) The rate must be greater than 0.00 and less than or equal to 0.20"
r = gets.chomp.to_f
while ((count <3) && (r < 0.00 || r > 0.20))
puts""
puts 'YOU MUST ENTER A VALID RATE. PLEASE SEE EXAMPLE ABOVE.'
puts ""
count = count + 1
puts ""
puts "What is the annual rate of interest? (ex. .08 = 8%) The rate must be greater than 0.00 and less than or equal to 0.20"
r = gets.chomp.to_f
while ((count <3) && (r < 0.00 || r > 0.20))
puts 'YOU MUST ENTER A VALID RATE. PLEASE TRY AGAIN.'
puts ""
count = count + 1
puts ""
puts "What is the annual rate of interest? (ex. .08 = 8%) The rate must be greater than 0.00 and less than or equal to 0.20"
r = gets.chomp.to_f
end
end
if (count >=3)
puts ""
puts "TRY AGAIN LATER..."
puts "" && exit
return r
end
count = 0
puts "How many years will the investment be for? (ex. 2 = 2 years) Please enter a number between 1 and 30."
n = gets.chomp.to_i
while ((count < 3) && (n < 1 || n > 30))
puts""
puts"YOU MUST ENTER AT LEAST ONE YEAR. PLEASE SEE EXAMPLE ABOVE."
puts""
count = count + 1
puts ""
puts "How many years will the investment be for? (ex. 2 = 2 years) Please enter a number between 1 and 30."
n = gets.chomp.to_i
while ((count < 3) && (n < 1 || n > 30))
puts""
puts"PLEASE ENTER A VALID NUMBER. PLEASE TRY AGAIN."
puts""
count = count + 1
puts""
puts "How many years will the investment be for? (ex. 2 = 2 years) Please enter a number between 1 and 30."
n = gets.chomp.to_i
end
end
if (count >= 3)
puts""
puts "TRY AGAIN LATER..."
puts "" && exit
return n
end
end
puts input
def final_value (fv)
pv = 1000 #want user's input here
r = 0.05 #want user's input here
n = 5 #want user's input here
fv = pv * (1+r) ** n
puts "After #{n} years you will have $#{fv.round(2)}!"
puts ""
puts "Press ENTER to exit."
end
puts final_value ()
gets
您可以定義這樣的方法:
def final_value (pv, r, n)
pv * (1+r) ** n
end
然后,使用不同的pv
、 r
、 n
值調用該方法。 例如
# pv = 100
# r = 0.5
# n = 10
# take input from user in console
puts 'Enter pv: '
pv = gets.strip.to_f
puts 'Enter r: '
r = gets.strip.to_f
puts 'Enter n: '
n = gets.strip.to_f
fv = final_value(pv, r, n)
puts "fv: #{fv}"
# => fv: 5766.50390625
你試過了嗎:
pv * (1+r) ** n
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.