LoginSignup
0
1

More than 3 years have passed since last update.

預金システムのアルゴリズム問題

Posted at

の条件を達成するプログラムを実装。

銀行口座に10万円の預金残高があり、お金を引き出すプログラムを作成します。
・お金を引き出すwithdrawメソッドを作成する
・お金を引き出すと手数料110円かかり、「◯◯円引き落としました。残高は◯◯円です」と表示する(残高は手数料を引いた額を表示します)
・もし預金残高より多く引き落としたら「残高不足です」と表示する

def withdraw(balance, amount)
  fee = 110  # 手数料
# 引き落とし額と残高を表示する、もしくは残高より多く引き落としたら残高不足と表示
end

balance = 100000  # 残高
puts "いくら引き落としますか?(手数料110円かかります)"
money = gets.to_i
withdraw(balance, money)
def withdraw(balance, amount)
  fee = 110
  if balance >= (amount + fee)
    balance -= (amount + fee)
    puts "#{amount}円引き落としました。残高は#{balance}円です"
  else
    puts "残高不足です"
  end
end

balance = 100000
puts "いくら引き落としますか?(手数料110円かかります)"
money = gets.to_i
withdraw(balance, money)

13行目で入力した金額(amount)と手数料110(fee)の合計が100000以上か否かを3行目〜8行目で条件づけをしています。100000以上の場合は、balance -= (amount + fee)、つまりbalance = balance - (amount + fee)が処理され、最終的な返り値が出力されます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1