Last active
February 28, 2017 01:33
-
-
Save k0mach1/b4b3ed785d6bd519d7aafe1e2e9f7d43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------------------------------------追加箇所↓ | |
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