Rubyをベースにプログラミングを学んでいる超初心者です。どうしてもエラーが解決しないので、皆さんの力をかりたいです。
Ruby
1#配列のなかに3文字以上の数字が入っている時、三番目に大きい数字を返すメソッドを作れ。 2 3def third_greatest(nums) 4first = nil 5second = nil 6third = nil 7 8 idx = 0 9 while idx < nums.length 10 value = nums[idx] 11 if first == nil || value > first then 12 third = second 13 second = first 14 first = value 15 elsif second == nil || value > second then 16 third = second 17 second = value 18 elsif third == nil || value > third then 19 third = value 20 end 21 22 idx += 1 23 end 24 25 return third 26end 27 28# These are tests to check that your code is working. After writing 29# your solution, they should all print true. 30 31puts( 32 'third_greatest([5, 3, 7]) == 3: ' + 33 (third_greatest([5, 3, 7]) == 3).to_s 34) 35puts( 36 'third_greatest([5, 3, 7, 4]) == 4: ' + 37 (third_greatest([5, 3, 7, 4]) == 4).to_s 38) 39puts( 40 'third_greatest([2, 3, 7, 4]) == 3: ' + 41 (third_greatest([2, 3, 7, 4]) == 3).to_s 42)
これを実行すると
lang
1$ ruby 10-third-greatest.rb 210-third-greatest.rb:15: syntax error, unexpected keyword_then, expecting keyword_end 310-third-greatest.rb:19: syntax error, unexpected keyword_elsif, expecting keyword_end 4 elsif second == nil || value > second then 5 ^ 610-third-greatest.rb:19: syntax error, unexpected keyword_then, expecting keyword_end 710-third-greatest.rb:22: syntax error, unexpected keyword_elsif, expecting keyword_end 8 elsif third == nil || value > third then 9 ^ 1010-third-greatest.rb:22: syntax error, unexpected keyword_then, expecting keyword_end 1110-third-greatest.rb:30: syntax error, unexpected keyword_end, expecting end-of-input
のようなエラーがでます。
ifのところに全角を使っている部分があるのではないか、というような指摘もいただき全角、半角に気をつけて書き直してもみたのですが解決できませんでした。どこが間違えているかわからないです。
誰か頭脳明晰な方、ご教授願います。
ちなみに使っているIDEはcloud9というものです。
このような状態になっています。
↓
回答8件
あなたの回答
tips
プレビュー