Guest User

Untitled

a guest
Feb 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. class Hundred
  2. def initialize(difficulty)
  3. @difficulty = difficulty
  4. @correct = 0
  5. @error = 0
  6. end
  7.  
  8. def question
  9. case @difficulty
  10. when 1
  11. "%d %c %d = " % [rand(10), [:+, :-, :*].choice, rand(10)].
  12. tap {|a, op, b| @ans = a.send(op, b) }
  13. when 2
  14. "%d %c %d = " % [rand(100), [:+, :-, :*, :/].choice, rand(100)].
  15. tap do |a, op, b|
  16. unless b == 0 || a < b || a % b != 0
  17. @ans = a.send(op, b)
  18. else
  19. retry
  20. end
  21. end
  22. end
  23. end
  24.  
  25. def answer(ans)
  26. if ans == @ans
  27. @correct += 1
  28. '正解'
  29. else
  30. @error += 1
  31. '不正解'
  32. end
  33. end
  34.  
  35. def result(start)
  36. "正解 :#{@correct}\n" +
  37. "不正解:#{@error}\n" +
  38. "時間 :#{Time.now - start} sec"
  39. end
  40.  
  41. end
  42.  
  43. if $0 == __FILE__
  44. puts '計算 100 開始'
  45. print '難易度を選択してください。1:かんたん 2:むずかしい > '
  46. difficulty = gets.chomp.to_i
  47. case difficulty
  48. when 1
  49. print '1:かんたんを開始します'
  50. when 2
  51. print '2:むずかしいを開始します'
  52. else
  53. puts '1か2を選択してください'
  54. exit 1
  55. end
  56. print ' plese enter > '
  57. gets
  58.  
  59. h = Hundred.new difficulty
  60.  
  61. start = Time.now
  62. 100.times do |i|
  63. print h.question
  64. puts h.answer gets.chomp.to_i
  65. puts
  66.  
  67. if (i+1) % 10 == 0
  68. puts "#{i+1}問突破"
  69. puts
  70. end
  71. end
  72.  
  73. puts h.result start
  74. end
Add Comment
Please, Sign In to add comment