我如何让我当前的ruby程序循环通过三个输入选项和显示,如果相同的选项已被使用两次?



我试图让我的当前程序给用户三个不同的输入选项,并通知用户,如果他们试图使用相同的输入选项两次。我当前的代码:

prompt = "Do you want to use the Subscription coupon[1] the one-time pay-per-use 
coupon[2] or the gift purchase coupon[3]: "
print prompt
code = gets.chomp
code = code.to_i
history = []
loop do
if code == 1
puts "Subscription coupon used"
print prompt
code = gets.chomp
elsif code == 2
puts "One-time pay-per-use coupon used"
print prompt
code = gets.chomp
elsif code == 3
puts "Gift purchase coupon used"
print prompt
code = gets.chomp
else history.include? code
puts "Code already input"
print prompt
code = gets.chomp
end
end

它允许我输入任意次数,但无论第二次输入是什么,它都会打印已经输入的代码文本。

如何修复代码以使其以预期的方式运行?

您可以考虑这样写。注意,我已经添加了一点

valid_entries = ["1", "2", "3", "4"]
answers = []
loop do
puts  "nDo you want to use the"
puts  "  Subscription coupon (1)"
puts  "  One-time pay-per-use coupon (2) or"
puts  "  Gift purchase coupon (3)?"
puts  "Enter (4) if you are finished"
print "Your selection: "
entry = gets.chomp
unless valid_entries.include?(entry)
puts "'#{entry}'is not a valid entry, you dummy. Try again."
next
end
if answers.include?(entry)
puts "You've already made that selection. Try again."
next
else
answers << entry
end
break if entry == "4"
puts case(entry)
when '1' then "Subscription coupon used"
when '2' then "One-time pay-per-use coupon used"
when '3' then "Gift purchase coupon used"
end
end

这是一个可能的对话。

Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 1
Subscription coupon used
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 2
One-time pay-per-use coupon used
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 1
You've already made that selection. Try again.
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: %
'%'is not a valid entry, you dummy. Try again.
Do you want to use the
Subscription coupon (1)
One-time pay-per-use coupon (2) or
Gift purchase coupon (3)?
Enter (4) if you are finished
Your selection: 4

作为

的替代
puts case(entry)
when '1' then "Subscription coupon used"
when '2' then "One-time pay-per-use coupon used"
when '3' then "Gift purchase coupon used"
end

你可以写

puts MESSAGES[entry]

在定义了常量 之后的

MESSAGES = { '1'=>"Subscription coupon used",
'2'=>"One-time pay-per-use coupon used"
'3'=>"Gift purchase coupon used" }

将历史检查移到循环的开头,并实际填充历史。以下是实现这一目标的众多方法之一:


loop do
if history.include?(code)
puts 'Code already input'
print prompt
code = gets.chomp.to_i
next # Stop processing this iteration of the loop immediately, and skip to the next iteration.
else
history << code
end
if code == 1
puts 'Subscription coupon used'
elsif code == 2
puts 'One-time pay-per-use coupon used'
elsif code == 3
puts 'Gift purchase coupon used'
else
puts 'Invalid entry.'  
end
print prompt
code = gets.chomp.to_i
end

最新更新