Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何重新提示和重新使用用户';s输入_Ruby_Loops_Prompt - Fatal编程技术网

Ruby 如何重新提示和重新使用用户';s输入

Ruby 如何重新提示和重新使用用户';s输入,ruby,loops,prompt,Ruby,Loops,Prompt,我试图重新提示用户的输入并重用它。下面是代码示例: print "Please put your string here!" user_input = gets.chomp user_input.downcase! if user_input.include? "s" user_input.gsub!(/s/,"th") elsif user_input.include? "" user_input = gets.chomp puts "You didn't enter anyt

我试图重新提示用户的输入并重用它。下面是代码示例:

print "Please put your string here!"

user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
  user_input.gsub!(/s/,"th")
elsif user_input.include? ""
  user_input = gets.chomp
  puts "You didn't enter anything!Please type in something."
  user_input = gets.chomp
else
  print "no \"S\" in the string"
end
puts "transformed string: #{user_input}!"

My
elsif
会让用户知道他们的输入是不可接受的,但在重新使用输入从头开始时效果不佳。我该怎么做?我应该使用
while
还是
进行
循环?

您可以在开始时使用循环不断请求输入,直到输入有效

while user_input.include? "" #not sure what this condition is meant to be, but I took it from your if block
    user_input = gets.chomp
    user_input.downcase!
end
这将持续请求输入,直到
user\u input.include?“”
返回false。这样,以后就不必验证输入了

然而,我不知道你在这里想做什么。如果要在输入为空时重新提示,可以使用条件
user\u input==“”


编辑:用于
字符串。包括?
。我试过运行
。是否包括?“”
和我得到空输入和非空输入的
true
。这意味着这将始终计算为
true

希望这能解决您的问题:)


我不知道您想做什么,但是对于任何字符串
s
s,包括吗?“
真的
@sawa,你当然不知道,因为你没有问他。你好,非常感谢!它起作用了!关于
next
方法,我只有一个问题。这是否意味着跳过上述条件并从顶部开始?不转到第二个条件?是的,“下一步”跳过当前迭代中的其余代码并开始下一个迭代。如果需要,可以省略
next
。对,您是@WayneConrad,因为在代码的caseBlocks之后没有其他语句。如果没有任何解释,看起来不正常。考虑添加一些,我只是用“循环做”来改变循环部分。首先,我使用“loop do”,因为我使用nill值启动用户输入,所以“loop do”仍然执行一次。它将循环,直到用户输入一个或多个输入,其余输入相同
while true
  print 'Please put your string here!'
  user_input = gets.strip.downcase

  case user_input
    when ''
      next
    when /s/
      user_input.gsub!(/s/, "th")
      puts "transformed string: #{user_input}!"
      break
    else
      puts "no \"S\" in the string"
      break
  end
end
  user_input = nil    
  loop do
      print "Please put your string here!"
      user_input = gets.chomp
      break if user_input.length>0
  end
  user_input.downcase!
  if user_input.include? "s"
     user_input.gsub!(/s/,"th")      
  else
     puts "no \"S\" in the string"
  end

  puts "transformed string: #{user_input}!"