当我尝试输入如下代码时:
print "Enter A Value: "
first_num = gets.to_i
print "Enter Another Value: "
second_num = gets.to_i
puts first_num.to_s + " + " + second_num.to_s + " = " + (first_num + second_num).to_s
print "Was That Correct? "
MyValue = gets.to_s
puts MyValue + "This is what you typed. Correct?"
在最后一个puts命令之后,程序直接关闭。有没有办法阻止这种情况发生?我希望程序保持打开状态,直到此人手动关闭它。不包括循环或击中输入。
答案 0 :(得分:2)
你可以做一个while
循环......继续运行程序,直到他们回复#34;是"纠正。
另请注意,以大写字母开头的变量是常量,因此在这种情况下,myValue
优先于MyValue
。
correct = nil
while correct != 'yes'
print "Enter A Value: "
first_num = gets.to_i
print "Enter Another Value: "
second_num = gets.to_i
puts first_num.to_s + " + " + second_num.to_s + " = " + (first_num + second_num).to_s
print "Was That Correct? "
myValue = gets.to_s
puts myValue + "This is what you typed. Correct?"
correct = gets.chomp.downcase
end
答案 1 :(得分:0)
您可以添加另一个gets
以使窗口保持打开状态,但真正的问题不是与Ruby语言有关,而是与您运行程序的方式有关。
如果只是希望程序不退出,您只需添加
即可loop { sleep 10 }
在你的节目结束时,这不是一个好主意,但听起来像你想要的。
如果您使用的是Windows,
=>运行程序后窗口没有关闭。
这是运行命令行程序的正确方法。