0
0

More than 3 years have passed since last update.

Ruby 始めました

Posted at

Rubyの勉強初めて2週間くらい。
右も左も分からない所からやっとそれらしい事ができるようになってきました。

qiita.rb
puts “いくら引き落としますか?“
count = gets.chomp.to_i
total_price = 10000 - count -110

if count <= total_price
 puts #{count}円引き落としました。残高は#{total_price}円です。”
else
 count >= total_price
 puts “残高不足です“
end

やっと出来た!と思ったら4000までは順調に動きましたが5000を入力すると表示されたのは残高不足・・・

あれ?

なんで?

まだ残高あるはずやろ?

何が間違ってるのかも分からなかった為、質問した結果

【count が5000のとき total_price = 10000 - count -110 は4890になるので count <= total_price は成立しませんね。】

最初は???となりましたがよく考えると理解出来ました。

ただどう修正すれば良いのかも分からなくなってしまったので一旦全削除。

作り直したのが

qiita.rb
def kouza(zandaka, withdraw)
  fee = 110
  if zandaka >= (withdraw + fee)
    zandaka -= (withdraw + fee)
    puts "#{withdraw}円引き落としました。残高は#{zandaka}円です"
  else
    puts "残高不足です"
  end
end

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

やっと出来ました。

いつどこで使うのか使いどころが分からないまま勉強してますが、とりあえず一歩前進。

0
0
1

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