如何在Ruby中将字符串转换为整数
我是ruby新手,正在尝试将字符串转换为整数 我试图计算GPA,所以我使用GET输入字母等级(a、B、C等),然后我将每个字母等级转换为各自的数字等级(a=4、B=3、C=2等)。我发现了一些关于如何将整数转换为字符串,而不是字符串转换为整数的信息。有什么建议吗如何在Ruby中将字符串转换为整数,ruby,Ruby,我是ruby新手,正在尝试将字符串转换为整数 我试图计算GPA,所以我使用GET输入字母等级(a、B、C等),然后我将每个字母等级转换为各自的数字等级(a=4、B=3、C=2等)。我发现了一些关于如何将整数转换为字符串,而不是字符串转换为整数的信息。有什么建议吗 puts ("This program will calculate your GPA this semester") puts ("Please type your grade, then press enter") put
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
更新:完全更改了代码。我想我是从错误的角度来的。但是,我仍然得到一个错误:grades.rb:10:nil:NilClass(NoMethodError)的未定义方法'last'
这里最简单的解决方案可能是散列:
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 ... ]
这里最简单的解决方案可能是散列:
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 ... ]
出现该错误的原因是第一次迭代时未定义
等级
。您没有意识到这一点,因为您在操作后放置了。你应该这样写
while grade.last != '' {
grade=gets.chomp
}
现在,除了这个循环没有做任何您希望它做的事情之外,这个表单要好得多,因为当它计算时,等级显然是nil
下面是您的代码的快速重写
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}"
还有一些其他方法可以做到这一点,但希望上面的内容足够简单,您可以看到您以前可能很难掌握的一般概念
我希望这对您有所帮助,但请询问您可能仍有疑问的任何问题。您出现此错误的原因是因为在第一次迭代中未定义等级。您没有意识到这一点,因为您在操作后放置了。你应该这样写
while grade.last != '' {
grade=gets.chomp
}
现在,除了这个循环没有做任何您希望它做的事情之外,这个表单要好得多,因为当它计算时,等级显然是nil
下面是您的代码的快速重写
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}"
还有一些其他方法可以做到这一点,但希望上面的内容足够简单,您可以看到您以前可能很难掌握的一般概念
我希望这对您有所帮助,但如果您还想知道,请询问任何问题。谢谢各位,我认为我从错误的角度编写了代码,没有使用数组。你们觉得怎么样?现在角度好点了吗?不过我还是有一个错误。查看原始帖子中的编辑和新代码。你应该编辑这个问题的标题,因为在搜索如何在Ruby中将字符串转换为int时,它在搜索结果中显示得很高,但这与此无关。谢谢大家,我认为我从错误的角度来编写代码,并且没有使用数组。你们觉得怎么样?现在角度好点了吗?不过我还是有一个错误。查看原始帖子中的编辑和新代码。你应该编辑这个问题的标题,因为在搜索如何在Ruby中将字符串转换为int时,它在搜索结果中显示得很高,但这与此无关。谢谢,伙计,你认为你可以帮我编辑?GRADE_值[get.chomp.strip]中的新代码吗当你做评分时,将把一个nil推到成绩表的末尾。您编写的循环将永远不会返回。谢谢,伙计,您认为您可以帮助编辑我在编辑中输入的新代码?GRADE_VALUES[gets.chomp.strip]将在执行GRADE_VALUES[''时将零推到成绩列表的末尾。您编写的循环将永远不会返回。此代码有点脆弱,即如果输入任何与值哈希中的等级不匹配的内容,它将失败。但它足以完成看似简单的家庭作业@费萨尔,上面的代码中有一个语法错误,但你应该能够找到并修复它。@Steve我意识到这一点的“脆弱性”,但我想,一旦费萨尔明白自己做错了什么,并找到更好的方法,我会把它留给他。Re:语法错误,除了愚蠢的gets.chomp.to\u I
之外,我没有看到任何东西,它可能就是gets.to\u I
。你指的是什么?这段代码有点脆弱,也就是说,如果输入任何与值哈希中的分数不匹配的内容,它将失败。但它足以完成看似简单的家庭作业@费萨尔,上面的代码中有一个语法错误,但你应该能够找到并修复它。@Steve我意识到这一点的“脆弱性”,但我想,一旦费萨尔明白自己做错了什么,并找到更好的方法,我会把它留给他。Re:语法错误,除了愚蠢的gets.chomp.to\u I
之外,我没有看到任何东西,它可能就是gets.to\u I
。你指的是什么?