0
我是一個使用Ruby版本2.1.5p273及以下版本的newb Ruby用戶我創建了一個Atm模擬器程序,它接受用戶輸入存款和取款,然後顯示餘額。我正在與ifs,elses和循環鬥爭。我想在開始時提出一個決策聲明,詢問用戶是否想要提取,存款,查看餘額或結束會話。我還希望最終做出決策聲明,詢問用戶是否想要繼續(這將返回到開始或結束會話)。我對我想要的樣子的一般想法會在下面,整個程序在想法代碼下面。我知道這是錯誤的,但它正是我想要它的樣子,所以任何幫助將它變成正確和有效的代碼將不勝感激。Ruby程序幫助(ATM程序)
print "Would you like to (w)ithdraw, (d)eposit, or (c)heck your balance or (e)nd your session?
if "(w)ithdraw" # i'd like to make this do a "press w for withdraw"
bank_account.withdraw
elsif "(d)eposit" # i'd like to make this do a "press d for deposit"
bank_account.deposit
elsif "(c)heck your balance" # i'd like to make this do a "press c to check your balance"
bank_account.show_balance
elseif "(e)nd your session" # i'd like to make this do a "press e to end your session"
end
#This program is an ATM simulator, it takes user input of deposits and withdrawals, and then displays the balance after.
class BankAccount
def initialize(name)
@transations = []
@balance = 0
end
def deposit
print "How much would you like to deposit? "
amount = gets.chomp
@balance += amount.to_f
puts "$#{amount} deposited."
end
def withdraw
print "How much would you like to withdraw?"
amount = gets.chomp
@balance -= amount.to_f
puts "#{amount} withdrawn"
end
def show_balance
puts "Your balance is #{@balance}"
end
end
bank_account = BankAccount.new("Justin G")
bank_account.class # => BankAccount
print "Welcome to Jay's ATM!\n"
bank_account.deposit
bank_account.show_balance
bank_account.withdraw
`enter code here`bank_account.show_balance
puts "Thank you"
太謝謝你了!這是完美的,我保存在兩個.rb文件,atm.rb和bank_account.rb,運行它在CMD和所有功能正常工作,存款取消支票餘額和結束。我要求的主要捷徑也在那裏。接下來我要做的是添加一個輸出收據文件。我會自己解決這個問題,如果我遇到麻煩,我希望你會再次出現。再次感謝你! – jmgeronimo 2014-12-06 04:19:04
@jmgeronimo不客氣。感謝您接受答案。我希望你喜歡學習Ruby。 – 2014-12-06 04:25:15