2016-01-27 26 views
0

我做了一個簡單的測驗,想知道如何通知用戶正確答案的數量,如果可能的話不正確,他們在答案結束時得到的答案測驗。以下是我的代碼。如何在測驗中告訴用戶正確答案的數量

prompt = "> " 
puts "Planet Pop Quiz, is the Earth round?" 
print prompt 

while user_input = gets.chomp 
    case user_input 
    when "yes" 
    puts "Correct!" 
    break 
    when "no" 
    puts "Wrong!" 
    break 
    else 
    puts "Please answer either yes or no" 
    print prompt 
    end 
end 

prompt = "> " 
puts "Is Pluto close to the sun?" 
print prompt 

while user_input = gets.chomp 
    case user_input 
    when "yes" 
    puts "Wrong!" 
    break 
    when "no" 
    puts "Correct!" 
    break 
    else 
    puts "Please answer either yes or no" 
    print prompt 
    end 
end 

prompt = "> " 
puts "Is Mercury bigger than Jupiter?" 
print prompt 

while user_input = gets.chomp 
    case user_input 
    when "yes" 
    puts "Wrong!" 
    break 
    when "no" 
    puts "Correct!" 
    break 
    else 
    puts "Please answer either yes or no" 
    print prompt 
    end 
end 
+0

爲了簡化,使用'gets gets.chomp'封裝'loop do ...'。你已經擺脫了循環。 – tadman

回答

0

開始通過在你的代碼的頂部聲明兩個變量:

correct = 0 
incorrect = 0 

然後對於每個switch語句,添加correct += 1incorrect += 1到各自when部分。

通過你的代碼的最後,你應該能夠輸出:

puts "You got #{correct} answers correct and #{incorrect} answers wrong." 

變量賦值和字符串插值是非常簡單的。我建議你go here並按照「入門」中的鏈接。

+0

非常感謝sjagr,這非常有幫助和簡單!非常感謝您的時間和專業知識! –

0

你可以把你的問題放在一個數組中,這樣你就可以刪除重複的詢問用戶響應的代碼,並且在這個過程中保持正確和錯誤的答案。這裏是做的一個方法:

prompt = "> " 

quiz = [ 
    {q: "Is the Earth round?", a: "yes"}, 
    {q: "Is Pluto close to the sun?", a: "yes"} 
] 

result = {:right => 0, :wrong => 0} 

puts "Planet Pop Quiz:" 

quiz.each.with_index do |h, i| 
    puts "Q#{i+1}: #{h[:q]}" 
    print prompt 

    while user_input = gets.chomp 
     case user_input 
      when "yes", "no" 
       if user_input == h[:a] 
        puts "Correct!" 
        result[:right] += 1 
       else 
        puts "Wrong!" 
        result[:wrong] += 1 
       end 
       break; 
      else 
       puts "Please answer either yes or no" 
       print prompt 
     end 
    end 
end 

print "You answered #{result[:right]} questions correctly out of #{questions.size} questions" 
+0

非常感謝你魔杖製造商!我需要研究一下,但看起來我會從你的建議中學習一些新的東西。感謝您的時間和專業知識! –

0

有一兩件事值得吸收首先編寫Ruby代碼是學習到的代碼方面更多的結構您的問題數據操作少的條件時。

例如:

# Define your questions in an array-of-arrays 
@questions = [ 
    [ 'Is Mars a planet?', 'yes' ], 
    [ 'Is the Sun a star?', 'yes' ], 
    [ 'Is Pluto a planet?', 'maybe' ] 
] 
@answers = [ 'yes', 'no', 'maybe' ] 
@responses = [ ] 

然後你可以遍歷這些容易:

@questions.each do |question, answer| 
    puts question 

    loop do 
    response = line.gets.chomp.downcase 

    if (@answers.include?(response)) 
     @responses << response 
     break 
    else 
     puts "Please respond with one of: #{@answers.join(', ')}" 
    end 
    end 
end 

# Keep score by true or false on answer matching 
@score = { true: 0, false: 0 } 

# Now step over each response and tally up score 
# according to the expected answer. 
@responses.each_with_index do |response, i| 
    @score[response == @questions[i][1])] += 1 
end 

puts "You got %d correct and %d incorrect" % [ 
    @score[true], 
    @score[false] 
] 

請注意,在這裏你可以操縱各種參數,而無需實際更改任何代碼。這允許很大的靈活性,並提供簡單的路徑來擴展此功能。例如,您可以輕鬆地從JSON或YAML文件中讀取問題。

+0

謝謝你tadman!這是非常有用的信息,我需要做我的功課和練習更多。感謝您的時間和專業知識! –

相關問題