Ruby 初心者スレッド Part 23at TECH
Ruby 初心者スレッド Part 23 - 暇つぶし2ch173:デフォルトの名無しさん
08/12/05 12:24:58
採点用プログラムを書いてるんですが、得点ごとの振り分けでちゃんと動きません。

【ちゃんと動くプログラム】
if 90<=tokuten ; foo[9] += 1 ; end
if 80<=tokuten && tokuten<90 ; foo[8] += 1 ; end
if 70<=tokuten && tokuten<80 ; foo[7] += 1 ; end
if 60<=tokuten && tokuten<70 ; foo[6] += 1 ; end
if 50<=tokuten && tokuten<60 ; foo[5] += 1 ; end
if 40<=tokuten && tokuten<50 ; foo[4] += 1 ; end
if 30<=tokuten && tokuten<40 ; foo[3] += 1 ; end
if 20<=tokuten && tokuten<30 ; foo[2] += 1 ; end
if 10<=tokuten && tokuten<20 ; foo[1] += 1 ; end
if 0<=tokuten && tokuten<10 ; foo[0] += 1 ; end

【上記では長いのでイテレータで短くしたプログラム(正しく動かない)】
(1..8).each do |i|
if (90<=tokuten && tokuten<=100) ; foo[9] += 1 ; end
if (i*10)<=tokuten && tokuten<(i+1)*10 ; foo[i] += 1 ; end
if 0<=tokuten && tokuten<10 ; foo[0] += 1 ; end
end

配列はちゃんと0で初期化してあります。
短くしたプログラムはfoo[9]以外はちゃんと動いてるみたいです。
foo[9]だけが明らかにおかしい人数になるので不思議です・・・
なんとなくかっこで囲んでみたけどダメでした。もちろん上記と同じように「90<=tokuten」も試しましたがダメでした。
助言お願いします。


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch