回答編集履歴
1
追記しました。
answer
CHANGED
@@ -29,4 +29,34 @@
|
|
29
29
|
|
30
30
|
times(5)
|
31
31
|
|
32
|
+
```
|
33
|
+
|
34
|
+
個人的にはメソッド内にメソッドを作成するよりは、以下のようにした方が好みです。
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
def times (count)
|
38
|
+
goukei = 0
|
39
|
+
count.times do
|
40
|
+
kingaku = okane(1000,300)
|
41
|
+
puts("掛かった金額は#{kingaku}円")
|
42
|
+
goukei += kingaku
|
43
|
+
end
|
44
|
+
puts("平均金額は#{goukei/count}円")
|
45
|
+
end
|
46
|
+
|
47
|
+
def okane(prob,cost)
|
48
|
+
times=0
|
49
|
+
|
50
|
+
while true
|
51
|
+
times+=1
|
52
|
+
|
53
|
+
if rand(prob)==0
|
54
|
+
return times*cost
|
55
|
+
else
|
56
|
+
#puts("はずれ!")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
times(5)
|
32
62
|
```
|