teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字

2020/10/30 11:05

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes

1

誤字

2020/10/30 11:04

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- メソッドさらにメソッドで返す
1
+ 金額の計算の合計もとめる
body CHANGED
@@ -1,4 +1,3 @@
1
- 下のメソッドを、さらに引数で指定した回数実行した結果の平均金額を返すメソッドを定義したいのですがなかなかできません。
2
1
  ```
3
2
  def okane(prob,cost)
4
3
  times=0
@@ -16,44 +15,4 @@
16
15
 
17
16
  puts(okane(1000,300))
18
17
 
19
- ```
20
-
21
-
22
- count.times doをつかって指定した回数結果を表示できるようになりましたが、それを足してさらに平均金額を出すことができません。
23
- ```
24
-
25
- def times (count)
26
- count.times do
27
-
28
- def okane(prob,cost)
29
- times=0
30
-
31
- while true
32
- times+=1
33
-
34
- if rand(prob)==0
35
- return "掛かった金額は#{times*cost}円"
36
- else
37
- #puts("はずれ!")
38
- end
39
- end
40
- end
41
-
42
- puts(okane(1000,300))
43
-
44
- end
45
-
46
-
47
- end
48
-
49
- times(5)
50
-
51
- ```
52
- 上の実行結果
53
- ```
54
- 掛かった金額は431400円
55
- 掛かった金額は388800円
56
- 掛かった金額は88500円
57
- 掛かった金額は13200円
58
- 掛かった金額は1054500円
59
18
  ```