Ruby-无法使用数组整数执行计算

时间:2018-08-18 08:27:04

标签: ruby

我会先告诉你我是一个初学者,然后再开始。我正在尝试构建一个简单的Ruby应用程序来计算债务支付。现在,它只是一个命令行程序。

程序首先开始询问您的债务清单的名称(通过btn_list_name方法),并使用用户输入清单名称的键和数组来设置哈希,以保存通过add_debt_item方法传递给它的信息。

我遇到错误的地方是付款时间表。错误不是字符串到整数的隐式转换。我为此感到困惑,因为在add_debt_item方法中,用户输入是使用to_i方法捕获的。

我期望该方法的行为是遍历每个债务哈希。对于每个债务,输出应说明余额,从余额中减去min_payment,然后继续循环进行该计算,直到余额达到零。我不确定是否可以使用case语句,但这是我尝试解决此当前错误的最新想法。

余额为500,最低还款额为100的情况如下:

Payment 1: 100
Payment 2: 100
Payment 3: 100
Payment 4: 100
Payment 5: 100

我想提出一些批评,但请耐心接受我的新手理解。感谢您提供的所有帮助!

class Ds

def initialize
  @debts = []
end

def debt_list_name
  print "What is the name of your Debt List? "
  name = gets.chomp.capitalize
  @debt_list = {"name" => name, "debts" => @debts }
end

def ask
  loop do
    print "Would you like to add a debt to your snowball? (y/n) "
    answer = gets.chomp.downcase.to_s
    if answer == "n"
      break
    elsif answer == "y"
      add_debt_item()
    else answer != "y" || answer != "n"
      puts "Please type y or n"
    end
  end
end

def add_debt_item()
  print "What is the name of your source of debt? "
  institution = gets.chomp.capitalize
  print "What is the balance of #{institution}? "
  balance = gets.chomp.to_i
  print "What is your current minimum payment for #{institution}? "
  min_payment = gets.chomp.to_i

  hash = {"institution" => institution, "balance" => balance.to_i, "min_payment" => min_payment.to_i}
  @debts.push(hash)

end

def print_debt_list(debt_list)
  puts "Debt list: " + debt_list['name']
  puts "Institution" + "\t\t" + "Balance" + "\t\t" + "Minimum Payment"
  puts "-"*60
  debt_list["debts"].each do |debt|
    puts debt["institution"] + "\t"*3 + debt["balance"].to_s + "\t"*2 + debt["min_payment"].to_s
  end
end

def payment_schedule(debt_list)
  balance = debt_list["balance"].to_i
  min_payment = debt_list["min_payment"].to_i
  debt_list["debts"].each do |debt|
    loop do
      #if debt["balance"].to_i - debt["min_payment"].to_i > debt["min_payment"].to_i
        i ||= 1
        case balance <= min_payment
        when true
          puts "Payment: #{i}: " + balance
          break
        when false
          puts "Payment: #{i}: " + min_payment
          balance -= min_payment
          i += 1
        end
      #elsif debt["balance"].to_i - debt["min_payment"].to_i < debt["min_payment"].to_i
    end
  end
end


# instantiate class
ds = Ds.new
# sets up the debt list
debt_list = ds.debt_list_name
# input to ask user what debts they'd like to add
ds.ask
# prints out the contents of the debt list
ds.print_debt_list(debt_list)
# prints out the payment schedule
ds.payment_schedule(debt_list)


# end class
end

1 个答案:

答案 0 :(得分:0)

我建议打印一个debt_list样本作为示例:

p ds.debt_list

然后,将其复制并粘贴到单独文件中的var中。处理它以编写所需的方法。因此,您无需运行整个应用程序(通过终端)。您可以直接在Sublime cmd + B的最喜欢的代码编辑器中运行和调试。

假设您打印了此列表,它是一个哈希值:

debt_list = {"name"=>"ListName", "debts"=>[{"institution"=>"A", "balance"=>1005, "min_payment"=>100}, {"institution"=>"B", "balance"=>5000, "min_payment"=>500}]}

独立进行研究:例如,一种方法可能是:

debt_list["debts"].each do |debt|
  puts "Payment schedule for #{debt["institution"]}"
  balance, min_payment = debt["balance"], debt["min_payment"]
  puts "#{balance} - #{min_payment}"
  num_payments = balance / min_payment
  last_payment = balance % min_payment # its the remainder
  num_payments.times do |n|
    puts "Payment #{n + 1}: #{min_payment}"
  end
    puts "Payment #{num_payments + 1}: #{last_payment}" unless last_payment == 0
end

一旦按预期工作,将其包装在您的类方法中并测试完整的应用程序。

编辑----调试代码

这是您的代码,运行如上所述的bts_list。作为注释进行解释。

def payment_schedule(debt_list)
  # balance = debt_list["balance"].to_i # this hash key does not exist at first level
  # min_payment = debt_list["min_payment"].to_i # this hash key does not exist at first level
  debt_list["debts"].each do |debt|
    balance = debt["balance"] # place assignement inside the each loop, here the hsh key exists -- add .to_i in case not already done in gets
    min_payment = debt["min_payment"] # place assignement inside the each loop, here the hsh key exists -- add .to_i in case not already done in gets
    puts "---- balance: #{balance} ---- min_payment: #{min_payment}" # added this line just as header
    i = 1 # i must be assigned outside the loop or it is restored each time
    loop do
      case balance <= min_payment
      when true
        puts "Payment: #{i}: " + balance.to_s # needs to convert balance to string, like this, or intrpolate
        break
      when false
        puts "Payment: #{i}: #{min_payment}" # needs to convert min_payment to string or interpolate, like this
        balance -= min_payment
        i += 1
      end
    end
  end
end

payment_schedule(debt_list)