0
我有一個數組 loan = %w(100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000)
我想.sample
這個數組並存儲它以備後用。將一個隨機選取的元素保存在一個數組中
我創建一個ATM程序,並正在創建貸款類..
來源:
require_relative 'loanamount.rb' #Where the above array is stored
private #Is part of an ATM class, which has nothing to do with this section
class Loan < ATM
attr_accessor :credit
def initialize(score)
@score = 0
end
end
def loan_info
puts <<-END.gsub(/^\s*>/, ' ')
>
>Hello and welcome to the credit station
>Please choose from the list below
>Would you like to Apply for a loan '1'
>Check credit score '2'
>Go back '3'
>
END
input = gets.chomp
case input.to_i
when 1
apply_credit
when 2
check_score
else
redirect
end
end
def apply_credit
if @score >= 640
accepted
else
denied_loan
end
end
def accepted
puts "You have been accepted for a #{loan.sample} loan which will be added to your bank account"
puts <<-END.gsub(/^\s*>/, ' ')
>
>Which account would you like to add that to?
>Checking Account '1'
>Savings Account '2'
>
END
input = gets.chomp
case input.to_i
when 1
@checking_account += "#{loan}"#I want to add the amount that loan.sample gave
puts "#{loan} has been added to your checking account, your new balance is #{@checking_account}"
puts "Your card will now be returned for security purposes."
exit
when 2
@savings_account += "#{loan}" #Not completed yet..
end
end
因此,例如:
loan = ["100", "200", "300"]
puts "You are given #{loan.sample}"
puts "You now have *amount*" #I want to be able to call the amount that loan.sample gave me"
我知道,代碼會在所有輸出的整個陣列,或什麼都沒有。你回答我的問題,所以謝謝你 – 13aal
希望有所幫助。 Ruby對於事物的嚴格程度可能有點令人困惑,但你會得到它的訣竅。 – tadman
我越來越好這就是所有重要的大聲笑! – 13aal