如何告诉用户测验中正确答案的数量

时间:2016-01-27 03:47:23

标签: ruby

我做了一个简单的测验,并且想知道如何告知用户在测验结束时他们得到的正确答案,如果可能的话,答案是否正确。以下是我的代码。

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

3 个答案:

答案 0 :(得分:0)

首先在代码顶部声明两个变量:

correct = 0
incorrect = 0

然后,对于每个switch语句,将correct += 1incorrect += 1添加到相应的when部分。

在代码结束时,您应该能够输出:

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

变量赋值和字符串插值非常简单。我建议您go here并按照“使用入门”下的链接进行操作。

答案 1 :(得分: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"

答案 2 :(得分: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文件中读取问题。