有没有更好的方法来缩短这些重复的 switch case 语句?也许,一个更具可读性的解决方案?
isTyping = true
accMenu = ['Balance', 'Withdraw', 'Deposit', 'Exit']
account = Account.new('Jason Bourne', 278430, 100.0)
while isTyping do
accMenu.each_with_index { |str, i| print i+1, '. ', str, "\n" }
print 'Enter account option: '
accOption = gets.to_i
case accOption
when 1
puts "Account Balance: #{account.balance}"
when 2
puts 'How much to withdraw from account? '
debit = gets.to_f
account.withdraw(debit)
when 3
puts 'How much to deposit from account? '
credit = gets.to_f
account.deposit(credit)
when 4
isTyping = false
else
puts 'Invalid account option, try again!'
end
end