在 Ruby 中没有收到输入
gets not receiving input in Ruby
我有一个哨兵控制循环,它似乎不接受任何输入:
response = " "
until response == "x" do
puts"
Xargonia
=========
(N)ew
(L)oad
(O)ptions
(Q)uit"
reponse = gets
puts response
当它试图输出用户输入的内容时,它只输出用于初始化的内容。
除了语法错误,您将 gets
的值分配给 reponse
而不是 response
。另外,您需要通过链接 String::chomp
:
从 gets
中删除换行符
response = " "
until response == "x" do
puts " # add space between gets and double-quote
Xargonia
=========
(N)ew
(L)oad
(O)ptions
(Q)uit"
response = gets.chomp # rename reponse to response; chain .chomp
puts response
end # add missing end
我有一个哨兵控制循环,它似乎不接受任何输入:
response = " "
until response == "x" do
puts"
Xargonia
=========
(N)ew
(L)oad
(O)ptions
(Q)uit"
reponse = gets
puts response
当它试图输出用户输入的内容时,它只输出用于初始化的内容。
除了语法错误,您将 gets
的值分配给 reponse
而不是 response
。另外,您需要通过链接 String::chomp
:
gets
中删除换行符
response = " "
until response == "x" do
puts " # add space between gets and double-quote
Xargonia
=========
(N)ew
(L)oad
(O)ptions
(Q)uit"
response = gets.chomp # rename reponse to response; chain .chomp
puts response
end # add missing end