Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Hundred
- def initialize(difficulty)
- @difficulty = difficulty
- @correct = 0
- @error = 0
- end
- def question
- case @difficulty
- when 1
- "%d %c %d = " % [rand(10), [:+, :-, :*].choice, rand(10)].
- tap {|a, op, b| @ans = a.send(op, b) }
- when 2
- "%d %c %d = " % [rand(100), [:+, :-, :*, :/].choice, rand(100)].
- tap do |a, op, b|
- unless b == 0 || a < b || a % b != 0
- @ans = a.send(op, b)
- else
- retry
- end
- end
- end
- end
- def answer(ans)
- if ans == @ans
- @correct += 1
- '正解'
- else
- @error += 1
- '不正解'
- end
- end
- def result(start)
- "正解 :#{@correct}\n" +
- "不正解:#{@error}\n" +
- "時間 :#{Time.now - start} sec"
- end
- end
- if $0 == __FILE__
- puts '計算 100 開始'
- print '難易度を選択してください。1:かんたん 2:むずかしい > '
- difficulty = gets.chomp.to_i
- case difficulty
- when 1
- print '1:かんたんを開始します'
- when 2
- print '2:むずかしいを開始します'
- else
- puts '1か2を選択してください'
- exit 1
- end
- print ' plese enter > '
- gets
- h = Hundred.new difficulty
- start = Time.now
- 100.times do |i|
- print h.question
- puts h.answer gets.chomp.to_i
- puts
- if (i+1) % 10 == 0
- puts "#{i+1}問突破"
- puts
- end
- end
- puts h.result start
- end
Add Comment
Please, Sign In to add comment