#問題
野球のルールをrubyで書きます。
strikeが入力されれば、strike!
ball が入力されれば、ball!
3回目のstrikeの時は、 out!
4回目のball の時は、 fourball!
と出力したいです。
#問題点
以下のコードだとstrikeと入力しても、ball!と出力されます。どうしてでしょうか?
ruby
1input = gets.to_i 2number_of_strike = 0 3number_of_ball = 0 4throw = 0 5input.times do |throw| 6 throw = gets 7 if throw == "strike" 8 number_of_strike += 1 9 if number_of_strike == 3 10 puts "out!" 11 else 12 puts "strike!" 13 end 14 else 15 number_of_ball += 1 16 if number_of_ball == 4 17 puts "fourball!" 18 else 19 puts "ball!" 20 end 21 end 22end
5 ball strike ball strike strike
ball! ball! ball! fourball! ball!
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。