简体   繁体   English

在Ruby中定义方法后如何运行计算?

[英]How to run calculation after methods are defined in Ruby?

Trying to find the future value of a single amount.试图找到单个金额的未来价值。

I have created and defined the present value, rate and years.我已经创建并定义了现值、比率和年数。 They all work together and now I am trying to figure out how to take the numbers entered for each and calculate them for the final value.他们都一起工作,现在我试图弄清楚如何获取为每个输入的数字并计算它们的最终值。

The formula I am using is:我使用的公式是:

(fv = pv * (1+r) ** n)

but am unsure how to get Ruby to run the calculation?但我不确定如何让 Ruby 运行计算?

Here is my program code:这是我的程序代码:

#CALCULATION OF FUTURE VALUE FOR A SINGLE AMOUNT #单个金额的未来价值计算

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

You can define a method like this:您可以定义这样的方法:

def final_value (pv, r, n)
    pv * (1+r) ** n
end

And, then call the method with different pv , r , n values.然后,使用不同的pvrn值调用该方法。 eg例如

# 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

Did you try:你试过了吗:

pv * (1+r) ** n

? ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM