【发布时间】:2015-12-06 19:56:43
【问题描述】:
我一直在尝试在 ruby 中创建一个响应用户输入的 while 循环。 应该发生的是,当用户正确输入“Omega”这个词时,它会结束循环并显示一条消息,如果输入“提示”这个词,则会显示一条消息并重复循环,其他任何东西都会有一个“重试”消息显示。发生的情况是,无论输入什么,循环都只会询问原始问题。
谁能找到我的代码有什么问题,谢谢
@GateOneLocked = true
GateOnePassword = String.new('Omega')
GateOneHint= String.new('hint')
#Omega is supposed to be the correct password
while (@GateOneLocked == true) do
puts 'What is the password?'
passwordEntered = gets.to_s
if (@passwordEntered == @GateOnePassword)
@GateOneLocked == false
else if (@passwordEntered != @GateOneHint)
puts "This is a hint: the password is 'Omega'"
puts " "
puts " "
else
puts "wrong password, try again"
puts " "
puts " "
end
end
end
puts 'You entered the correct password!'
【问题讨论】:
标签: ruby if-statement while-loop user-input