Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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/2/facebook/8.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中使用STDIN.gets.chomp()无法获取正确的输入_Ruby_Stdin - Fatal编程技术网

在ruby中使用STDIN.gets.chomp()无法获取正确的输入

在ruby中使用STDIN.gets.chomp()无法获取正确的输入,ruby,stdin,Ruby,Stdin,我正在学习LearnRubyTheHardWay教程,在修改该教程时遇到了困难。如果我定义以下变量(如教程中所示),一切都会正常工作: 但是,如果我尝试从STDIN获取变量,如: puts "How many people are here?" people = STDIN.gets.chomp() puts "How many cats?" cats = STDIN.gets.chomp() puts "And how many dogs?" dogs = STDIN.gets.chomp()

我正在学习LearnRubyTheHardWay教程,在修改该教程时遇到了困难。如果我定义以下变量(如教程中所示),一切都会正常工作:

但是,如果我尝试从STDIN获取变量,如:

puts "How many people are here?"
people = STDIN.gets.chomp()
puts "How many cats?"
cats = STDIN.gets.chomp()
puts "And how many dogs?"
dogs = STDIN.gets.chomp()
相等运算符返回错误的结果,就好像它们只使用数字的前两位数字计算结果一样。因此,如果我给人输入100000000,给猫输入11、12或13,如果我给人和猫输入15000000,方法会返回“太多猫…” “猫不多!世界得救了!” 结束 如果人们喜欢狗 写着“全世界都流口水了!” 结束 如果人>狗 写着“世界是干燥的!” 结束 狗+=“5” “现在有{狗}狗了。” 如果人>=狗 “人大于或等于狗。” 结束
如果people,问题是
STDIN.get
返回一个字符串。因此,所有比较操作都将对字符串进行操作。例如:

people = "100000000"
cats = "11"

puts people < cats  # => true!
如果您在此处输入
7
,它应该打印出
75
。你看,它只是连接字符串

如何解决这个问题?简单,只需将字符串转换为整数:

puts "How many people are here?"
people = STDIN.gets.to_i
puts "How many cats?"
cats = STDIN.gets.to_i
puts "And how many dogs?"
dogs = STDIN.gets.to_i

谢谢你,我试过用people.to_I,但现在我发现我忘了像“people=people.to_I”那样做。我没有见过像STDIN.geti而不是get这样的东西。除了rubydoc.org,还有什么地方可以推荐你去学习其他的方法吗?@barerd:没有像
STDIN.geti
这样的东西。您使用
gets.to_i
,因此在STDIN file stream对象上调用该方法,然后在生成的
String
对象上调用该方法。如果您想对可用的方法进行概述,可以使用比rubydoc更结构化的方法。不过,最好先对该语言有一个基本的了解(教程对此很有帮助),Apidock将是对教程的一个很好的补充。也谢谢你。
dogs += "5"
puts "How many people are here?"
people = STDIN.gets
puts "How many cats?"
cats = STDIN.gets
puts "And how many dogs?"
dogs = STDIN.gets

#people = 100000
#cats = 34
#dogs = 56

puts "So, %d people, %d cats and %d dogs, huh?" % [people,cats,dogs]

if people < cats
  puts "Too many cats! The world is doomed!"
end

if people > cats
  puts "Not many cats! The world is saved!"
end

if people < dogs
  puts "The world is drooled on!"
end

if people > dogs
  puts "The world is dry!"
end

dogs += "5"
puts "Now there are #{dogs} dogs."

if people >= dogs
  puts "People are greater than or equal to dogs."
end

if people <= dogs
  puts "People are less than or equal to dogs."
end

if people == dogs
  puts "People are dogs."
end
people = "100000000"
cats = "11"

puts people < cats  # => true!
dogs = STDIN.gets
dogs += "5"
puts "How many people are here?"
people = STDIN.gets.to_i
puts "How many cats?"
cats = STDIN.gets.to_i
puts "And how many dogs?"
dogs = STDIN.gets.to_i