【问题标题】:`gets`not working when used in `if...end`statement在 `if...end` 语句中使用时,`gets` 不起作用
【发布时间】:2013-01-24 03:56:27
【问题描述】:

我想找到一种方法让我的脚本等到用户点击 ENTER,同时使用 if...end

input = 3

if input > 2    
    puts "input is greater than 2"
    gets
    puts "this shouldn't appear before I type ENTER"
end

这不起作用,因为我得到了

$input is greater than 2
$this shouldn't appear before I type ENTER

我应该使用什么来代替gets 来暂停脚本?

感谢您的宝贵时间

【问题讨论】:

  • 您是从控制台阅读吗?还是来自文件?
  • 代码暂停对我来说很好。周围有更多代码吗?

标签: ruby


【解决方案1】:

尝试用$stdin.gets替换gets

【讨论】:

  • 为什么需要这个?
【解决方案2】:

它对我有用,您确定要从控制台读取吗?

input = 3

if input > 2    
    puts "input is greater than 2"
    puts "please enter your name"
    name = gets
    puts "hi #{name} this shouldn't appear before I type ENTER"
end

o/p

~/Desktop$ ruby demo.rb
input is greater than 2
please enter your name
salil
hi salil
 this shouldn't appear before I type ENTER

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2012-07-16
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    相关资源
    最近更新 更多