Skip to content

Instantly share code, notes, and snippets.

@k0mach1
Last active February 28, 2017 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0mach1/b4b3ed785d6bd519d7aafe1e2e9f7d43 to your computer and use it in GitHub Desktop.
Save k0mach1/b4b3ed785d6bd519d7aafe1e2e9f7d43 to your computer and use it in GitHub Desktop.
----------------------------------------------------------------------------------------------追加箇所↓
def int_valid?(string)
Integer(string)
return true
rescue ArgumentError
return false
end
----------------------------------------------------------------------------------------------
class Score
# 生徒のスコアを保存しておく配列
@@lists = []
def self.input
puts "生徒の名前を入力してください"
name = gets.chomp
puts "得点を入力してください"
----------------------------------------------------------------------------------------------追加箇所↓
input = gets
while int_valid?(input) == false
puts "入力された値は無効です"
puts "得点を入力してください"
input = gets
end
score = input.to_i
----------------------------------------------------------------------------------------------
# aという変数に判定の返り値を代入
# a = score.match(/\d/)
# while a == nil
# puts "入力された値は無効です"
# puts "得点を入力してください"
# score = gets.chomp
# a = score.match(/\d/)
# end
# score = score.to_i
data = Score.new(name, score)
@@lists << data
end
def initialize(name, score)
@name = name
@score = score
end
attr_accessor :name, :score
def self.show
# 結果表示
line = "------------------------"
puts line
total = 0
count = 0
@@lists = @@lists.sort{|a, b| b.score <=> a.score }
@@lists.each do |list|
puts "#{list.name} : #{list.score}"
total += list.score
count += 1
end
average = total/count
puts line
puts "平均得点 : #{average}"
end
end
while true do
puts "得点を入力しますか? : [0]Yes [1]No [2]Quit"
input = gets.to_i
if input == 0 then
Score.input
elsif input == 1 then
Score.show
elsif input == 2 then
exit
else
puts "入力された値は無効です"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment