【问题标题】:How to run calculation after methods are defined in Ruby?在 Ruby 中定义方法后如何运行计算?
【发布时间】:2016-01-08 01:28:26
【问题描述】:

试图找出单个金额的未来价值。

我已经创建并定义了现值、比率和年份。它们都一起工作,现在我试图弄清楚如何获取每个输入的数字并计算它们的最终值。

我使用的公式是:

(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

【问题讨论】:

    标签: ruby calc


    【解决方案1】:

    你可以这样定义一个方法:

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

    然后,使用不同的pvrn 值调用方法。例如

    # 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、r 和 n 值怎么办?如何存储用于此方法的条目?
    • 查看我的更新答案。您可以使用:gets 方法从用户那里获取输入,然后执行:get.strip 去除输入中的空格和换行符,然后使用.to_f 将字符串转换为浮点值。并且,将其保存在相应的变量中,如我更新的答案所示。如果您有任何其他问题,请告诉我。
    • 我在定义方法时遇到错误..." 'final_value': 参数数量错误(1 对 3)(ArgumentError)
    • 您将 1 个参数传递给 final_value 方法,但它需要 3 个。这样称呼它:final_value(100, 0.5, 10)
    • 它给了我一个语法错误,意外的 tINTEGER,期待 ') def final_value (100, 0.05, 10)
    【解决方案2】:

    你试过了吗:

    pv * (1+r) ** n
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2012-08-06
      相关资源
      最近更新 更多