为什么我不能将fixnum转换为字符串?

时间:2014-01-14 22:24:12

标签: ruby arrays if-statement

我正在用Ruby创建一个测验制作程序。它打开并读取文本文件,并能够在文本文件中对问题和答案进行测验。我试图让用户在完成测验后知道他们出了多少问题。

我得到的错误是:

quiz.rb:180:in `+': can't convert Fixnum into String (TypeError)
    from quiz.rb:180:in `<main>'

在经过所有问题后我得到了这个错误。

这是我的代码。每当有人提出错误的问题时,我会尝试从起始值10中减去1 correct_count。如果您想查看文本文件,请与我们联系。

questions = File.open("question.txt","r+")

array = {}

contents = questions.readlines

questions.close

contents.collect! do |x|
        x.chomp
end

contents.collect! do |x|
         x.split(',')
end

contents.each do |x|
        array[x[0]] = x
end

correct_count = 10


question1 = contents[0][1]
choice11 = contents[0][2]
choice12 = contents[0][3]
answer11 = contents[0][4]
question2 = contents[1][1]
choice21 = contents[1][2]
choice22 = contents[1][3]
answer21 = contents[1][4]
question3 = contents[2][1]
choice31 = contents[2][2]
choice32 = contents[2][3]
answer31 = contents[2][4]
question4 = contents[3][1]
choice41 = contents[3][2]
choice42 = contents[3][3]
answer41 = contents[3][4]
question5 = contents[4][1]
choice51 = contents[4][2]
choice52 = contents[4][3]
answer51 = contents[4][4]
question6 = contents[5][1]
choice61 = contents[5][2]
choice62 = contents[5][3]
answer61 = contents[5][4]
question7 = contents[6][1]
choice71 = contents[6][2]
choice72 = contents[6][3]
answer71 = contents[6][4]
question8 = contents[7][1]
choice81 = contents[7][2]
choice82 = contents[7][3]
answer81 = contents[7][4]
question9 = contents[8][1]
choice91 = contents[8][2]
choice92 = contents[8][3]
answer91 = contents[8][4]
question10 = contents[9][1]
choice101 = contents[9][2]
choice102 = contents[9][3]
answer101 = contents[9][4]
topic = contents[11][1]

puts "Welcome to this " + topic + " quiz. Please spell the answers exactly right to get them correct (don't worry about caps). Good Luck!"


puts question1
puts choice11 + " or " + choice12 + "?"

user1 = gets.chomp

if user1.downcase == answer11.downcase
      puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question2
puts choice21.downcase + " or " + choice22 + "?"

user2 = gets.chomp

if user2.downcase == answer21.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question3
puts choice31.downcase + " or " + choice32.downcase + "?"

user3 = gets.chomp

if user3.downcase == answer31.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question4
puts choice41.downcase + " or " + choice42.downcase + "?"

user4 = gets.chomp

if user4.downcase == answer41.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question5
puts choice51.downcase + " or " + choice52.downcase + "?"

user5 = gets.chomp

if user5.downcase == answer51.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question6
puts choice61.downcase + " or " + choice62.downcase + "?"

user6 = gets.chomp

if user6.downcase == answer61.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question7
puts choice71.downcase + " or " + choice72.downcase + "?"

user7 = gets.chomp

if user7.downcase == answer71.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question8
puts choice81.downcase + " or " + choice82.downcase + "?"

user8 = gets.chomp

if user8.downcase == answer81.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

if user9.downcase == answer91.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end

puts "Next question: " + question10
puts choice101.downcase + " or " + choice102.downcase + "?"

user10 = gets.chomp
if user10.downcase == answer101.downcase
        puts "correct"
else
        puts "incorrect" and correct_count -1
end


puts "you got" + correct_count + "out of 10 correct"

1 个答案:

答案 0 :(得分:3)

变化:

puts "you got" + correct_count + "out of 10 correct"

为:

puts "you got" + correct_count.to_s + "out of 10 correct"

或:

puts "you got #{correct_count} out of 10 correct"

注:

correct_count = 10
puts "incorrect" and correct_count -1
correct_count # => 10
# >> incorrect

correct_count仍评估为10

你应该这样做:

if user6.downcase == answer61.downcase
        puts "correct"
else
        puts "incorrect"
        correct_count -= 1
end