我試圖讓我當前的程式為用戶提供三個不同的輸入選項,并在他們嘗試使用相同的輸入選項兩次時通知用戶。我當前的代碼:
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
它允許我輸入任意多次,但無論第二個輸入是什么,它都會列印已經輸入文本的代碼。
如何修復代碼以使其按預期方式運行?
uj5u.com熱心網友回復:
您可以考慮如下撰寫。請注意,我添加了一些內容。
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" }
uj5u.com熱心網友回復:
將歷史檢查移到回圈的開頭,并實際填充歷史。這是實作這一目標的眾多方法之一:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/472667.html
