我正在尝试在提示中设置一个非常简单的骰子滚动和响应,但它无法正常工作。我希望循环继续询问,直到用户输入roll,Roll或ROLL。
def roll()
x = rand(5) + 1
y = rand(5) + 1
z = rand(5) + 1
puts "You rolled #{x},#{y},#{z}"
if x == y && y == z
puts "TRIPS! Trip #{x}s"
elsif x == y || y == z || x == z
puts "Pair!"
else
if x > y && x > z
puts "#{x} high"
end
if y > x && y > z
puts "#{z} high"
end
if z > x && z > y
puts "#{z} high"
end
end
end
puts "Type 'roll' when you're ready."
entry = gets.chomp
until entry == roll || Roll || ROLL
puts "Type 'roll' when you're ready."
entry = gets.chomp
end
roll()
答案 0 :(得分:2)
# Remove these two lines and just use them in the while / until
# puts "Type 'roll' when you're ready"
# entry = gets.chomp
entry = ''
# Use String.downcase on entry because the user could type rOLL or RoLl
while entry.downcase != 'roll' # or you can use until entry.downcase == 'roll'
puts "Type 'roll' when you're ready."
entry = gets.chomp
end
您正在将条目与不存在的值进行比较。你需要比较一个字符串。
按照惯例,滚动应该是某种类/模型/对象。例如
class Roll
# create roll
def initialize()
end
end
roll = Roll.new
# or for an existing class like Arrays
array = Array.new
按惯例,ROLL 将是先前定义的常量。
ROLL = 7
# => 7
ROLL = 4
# => warning: already initialized constant ROLL