This program demonstrates the use of hashes in Ruby, by mapping the students score (%) to GPA value.
=begin
This program demonstrates the use of hashes in Ruby, by mapping the students score (%) to GPA value.
=end
hash = {90 => 4, 80 => 3, 70 => 2, 60 => 1} # creates the new hash with key value pairs
puts "There are #{hash.length} possible passing grades one can receive" # prints the length of the hash
puts "Each of these affects one's GPA as follows"
puts "A score of 90 or above is assigned #{hash[90]} GPA points" # prints the value
puts "A score of 80 to 89 is assigned #{hash[80]} GPA points" # prints the value
puts "A score of 70 to 79 is assigned #{hash[70]} GPA points" # prints the value
puts "A score of 60 to 69 is assigned #{hash[60]} GPA points" # prints the value
begin
puts "Please enter student's score. (\"stop\" to quit)" # prints instructional message with the word stop within quotes
STDOUT.flush
score = gets.chomp
iscore = score.to_i # converts the input string to an integer
if score == 'stop'
puts "Ending Program"
elsif iscore >= 90
puts "Student awarded #{hash[90]} points toward GPA"
elsif iscore >=80
puts "Student awarded #{hash[80]} points toward GPA"
elsif iscore >=70
puts "Student awarded #{hash[70]} points toward GPA"
elsif iscore >=60
puts "Student awarded #{hash[60]} points toward GPA"
elsif iscore < 60
puts "No GPA points awarded to student"
end
end until score == 'stop'