晶体中的获取功能不等待用户输入.当我启动我的控制台应用程序时,它立即输出如下所示的错误.这是说给in_array函数的第二个参数是Nil,但程序甚至不要求用户输入.
我的代码如下所示.
# Only alice and bob are greeted.
def in_array(array : Array, contains : String)
array.each { |e|
if e == contains
return true;
end
}
return false;
end
allowed = ["Alice", "Bob"]
puts "Please enter your name to gain access."
name = gets
isAllowed = in_array(allowed, name)
if isAllowed
puts "You can enter the secret room"
else
puts "You are not allowed to enter the secret room."
end
Run Code Online (Sandbox Code Playgroud)
我的代码的新版本包括?和read_line
# Only alice and bob are greeted.
allowed = ["Alice", "Bob"]
puts "Please enter your name to gain access."
name = read_line.chomp
if allowed.includes?(name)
puts "You can enter the secret room"
else
puts "You are not allowed to enter the secret room."
end
Run Code Online (Sandbox Code Playgroud)
但是当我将Bob输入名称变量时,包括?method返回false并执行else语句.
| 归档时间: |
|
| 查看次数: |
430 次 |
| 最近记录: |