我做了一個簡單的測驗,想知道如何通知用戶正確答案的數量,如果可能的話不正確,他們在答案結束時得到的答案測驗。以下是我的代碼。如何在測驗中告訴用戶正確答案的數量
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
爲了簡化,使用'gets gets.chomp'封裝'loop do ...'。你已經擺脫了循環。 – tadman