0
0

More than 1 year has passed since last update.

Ruby if, else問題

Posted at

if, else問題

条件分岐を利用して、プログラムを作成していきます。
内容は下記の通りです。
ECサイトのポイント付与サービスを考えます。
購入金額が999円以下の場合、3%のポイント
購入金額が1000円以上の場合、5%のポイント
このように付与されるポイントを出力するメソッドを作りましょう。

ただし誕生日の場合はポイントが5倍になります。
誕生日の場合はtrue, 誕生日でない場合はfalseで表します。
また、小数点以下をすべてのポイント計算が終わったあとに切り捨てましょう。

模範解答
def calculate_points(amount, is_birthday)
  if amount <= 999
    point = amount * 0.03
  else
    point = amount * 0.05
  end
  if is_birthday
    point = point * 5
  end
  puts "ポイントは#{point.floor}点です"
end

# 呼び出し例
calculate_points(500, false)
calculate_points(2000, false)
calculate_points(3000, true)

ターミナルには以下のように出力されます。

ターミナル
ポイントは15点です
ポイントは100点です
ポイントは750点です

解説

ポイント

  • 購入金額によってポイント付与の割合が異なること
  • 誕生月は購入金額とは別でif文を使ってコードを書くこと

また、付与するポイントの計算結果については、小数点以下は切り捨てするという条件が提示されているため、.floorメソッドを使って切り捨てます。

0
0
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
0