2014-01-14 84 views
0

我正在Ruby中創建一個測驗製作程序。它打開並讀取文本文件,並能夠對文本文件中的問題和答案進行測驗。我試圖讓用戶知道他們在完成測驗後有多少問題。爲什麼我無法將fixnum轉換爲字符串?

我得到的錯誤是:

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

我得到這個錯誤它經歷了所有的問題之後。

這是我的代碼。 correct_count是我每次遇到問題時都會嘗試從開始10減去1的地方。讓我知道你是否想看到文本文件。

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" 
+0

此代碼是非常需要的幫助。我強烈建議在http://codereview.stackexchange.com上尋求幫助重構它。 –

回答

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 
+0

當我這樣做時,它說我得到10/10正確的,即使我得到他們全部正確 – Hazard17

+0

@ Hazard17:當你得到10個問題的正確答案時,它應該說「你有10個正確的」。有什麼問題? – Chuck

+0

@Chuck它說:「你有10個正確的10」,即使我得到他們所有不正確 – Hazard17

相關問題