获取Ruby中的.chop()用户输入错误



所以这是我的代码。这是不言自明的。

print "How old are you? "
age = gets.chomp()
print "How tall are you?"
height = gets.chomp()
print "How much do you weigh?"
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."

我的代码输出如下。

$  C:/Ruby200/bin/ruby.exe ex11.rb
11
11
11
How old are you? How tall are you?How much do you weigh?So, you're 11 old, 11 tall and 11 heavy.

这可能是一个非常简单的错误,但如果你能指出,我将不胜感激。

我认为你的问题是:"在所有输入之后,我的所有提示都会同时打印出来。这是怎么回事?"。我有一个答案给你:)

print不会在字符串中添加换行符。STDOUT在得到一整行之前不会冲洗。简单修复:将print替换为puts(它确实添加了换行符)

puts "How old are you? "
age = gets.chomp()
puts "How tall are you?"
height = gets.chomp()
puts "How much do you weigh?"
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."

相关内容

  • 没有找到相关文章

最新更新