如何让我当前的 ruby 程序循环遍历三个输入选项并在同一选项被使用两次时显示?
How do I get my current ruby program to loop through three input options and display if the same option has been used twice?
我正在尝试让我当前的程序为用户提供三个不同的输入选项,并在用户尝试使用相同的输入选项两次时通知他们。
我当前的代码:
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
它允许我输入任意多次,但无论第二个输入是什么,它都会打印已输入文本的代码。
如何修复代码以按预期方式将其发送到 运行?
将历史记录检查移到循环的开头,并实际填充历史记录。
这是实现该目标的众多方法之一:
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
你可以考虑这样写。请注意,我添加了一点。
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" }
我正在尝试让我当前的程序为用户提供三个不同的输入选项,并在用户尝试使用相同的输入选项两次时通知他们。 我当前的代码:
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
它允许我输入任意多次,但无论第二个输入是什么,它都会打印已输入文本的代码。
如何修复代码以按预期方式将其发送到 运行?
将历史记录检查移到循环的开头,并实际填充历史记录。 这是实现该目标的众多方法之一:
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
你可以考虑这样写。请注意,我添加了一点。
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" }