开发者

How to Convert String to Integer in Ruby

I'm new to ruby and am trying to convert a string into an integer.

I am trying to calculate GPA so I am using gets for input of a letter grade (A,B,C,etc.) then I will convert each of these into their respective number grades (A=4, B=3, C=2, etc.). I have found a bunch of info on how to convert integer to strings but not strings to integer. Any suggestions?

puts ("This program will calculate your GPA this semester")    
puts ("Please type your grade, then press enter")

puts("How many courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")

puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")

gradeList = []
gradeList.push gets.chomp while gradeList.last != ''
puts gradeList.sort

"A"=4
"A-"=3.7
"B+"=3.3

Update: Changed code completely. I think I was coming at it from the wrong angle. However, I am still getting an error:grades.rb:10: undefined method `last' for nil:NilClass (NoMethodError)

puts "This program will calculate your GPA this semester"
puts "Please type your grade, then press enter"

puts("How many 开发者_运维百科courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")

puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")

grade=gets.chomp while grade.last != ''

if grade == "A"
  total=total+4
elsif grade=="B"
  total=total+3
elsif grade=="C"
  total=total+2
elsif grade=="D"
  total=total+1
end

gpa=total/num_courses
puts"Your GPA is #{gpa}"


The reason you get that error is because grade is not defined on the first iteration. You don't realize this because you have the while positioned after the action. You should write it like this

while grade.last != '' {
  grade=gets.chomp
}

Now, other than the fact that this loop doesn't do anything that you want it to, this form is much better, because it becomes apparent that grade is nil when it evaluates.

Here is a quick re-write of your code...

puts "This program will calculate your GPA this semester"

puts "How many courses are you taking?"
num_courses = gets.chomp.to_i                    # num_courses is now an integer
puts "You are taking #{num_courses} courses"     # let's pretend num_courses = 4

puts "Use A, A-, B+, B, B-, C+, C, C-, D, or F"

values = {                          # using a hash will allow us to avoid a
    "A" => 4,                       # large and inefficient if / elsif statement
    "A-" => 3.7,
    "B+" => 3.3,
    "B" => 3,
}

total = 0.0                         # sets our total prior to the loop for scope
num_courses.times do                # so we will do this loop 4 times
    total += values[gets.chomp.upcase]       # looks up the value from our hash
end                                          # and adds it to the (running) total

gpa = total / num_courses           # calculates the gpa from the total
                                    # and the num_courses we asked earlier
puts "Your GPA is #{gpa}"

There are a few other ways you could do some of this, but hopefully the above is simple enough that you see the general concepts that you may have been struggling to grasp before.

I hope this helps you, but ask anything you might still wonder.


The simplest solution here would probably be a hash:

GRADE_VALUES = {
  "A" => 4,
  "A-" => 3.7,
  ...
}

gradeList = []
gradeList.push GRADE_VALUES[gets.chomp.strip] while gradeList.last != ''


puts gradeList

=> [3.7, 4, 3.3 ... ]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜