如何重新提示和重复使用用户的输入 [英] How to re-prompt and re-use a user's input

查看:79
本文介绍了如何重新提示和重复使用用户的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重新提示用户的输入并重新使用它.这是代码示例:

I was trying to re-prompt a user's input and reuse it. Here's the code sample:

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}!"

我的elsif将让用户知道他们的输入是不可接受的,但是对于从头开始重新使用他们的输入无效.我应该怎么做?我应该使用while还是for循环?

My elsif will let the user know that their input was not acceptable, but was not effective in re-using their input to start from the beginning. How am I supposed to do it? Should I use a while or for loop?

推荐答案

希望这可以解决您的问题:)

Hope this solves your problem :)

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

这篇关于如何重新提示和重复使用用户的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆