检查ruby中的整数是否为正

检查ruby中的整数是否为正,ruby,irb,Ruby,Irb,我试图让用户输入一个正整数(>=1)。如果用户输入负整数或0,程序会注意到并说您没有输入有效整数,请重试。我的程序工作到目前为止,直到我尝试输入一个字符串。我的程序在输入字符串后中断。有人知道怎么解决这个问题吗 我试过了!barsofsoap.match(/\A[-+]?[0-9]*.?[0-9]+\Z/) 输入“您没有输入整数,请输入正整数”,但不起作用 loop do puts "How many soaps are you looking to purchase? (Please pu

我试图让用户输入一个正整数(>=1)。如果用户输入负整数或0,程序会注意到并说您没有输入有效整数,请重试。我的程序工作到目前为止,直到我尝试输入一个字符串。我的程序在输入字符串后中断。有人知道怎么解决这个问题吗

我试过了!barsofsoap.match(/\A[-+]?[0-9]*.?[0-9]+\Z/) 输入“您没有输入整数,请输入正整数”,但不起作用

loop do
  puts "How many soaps are you looking to purchase? (Please put an integer)"
  x = gets.chomp
  barsofsoap = Integer(x) rescue false

  if barsofsoap >= 1
    break
  else
    puts "You didn't put an integer, please put an integer and try again."
  end
end

如果用户输入字符串,我希望程序不会中断。

您可以尝试以下操作:
num.positive?

请看这里:

您还可以尝试将文本转换为整数,并通过执行以下操作进行验证:
num.is\u是一个?(整数)

或者更好,因为输入总是文本,您可以使用正则表达式确保只输入数字:
“12345”=~/\a\d+\Z/

试试这个怎么样:

loop do
输入“您打算购买多少肥皂?(请输入整数)”
barsofsoap=get.chomp
如果barsofsoap.match(/^[\d]+$/).nil?
puts“您没有输入整数,请输入整数并重试。”
elsif barsofsoap.to_i.阳性?
打破
其他的
puts“您没有输入整数,请输入整数并重试。”
结束
结束

当前代码失败的原因是您使用了
false
作为救援值。您无法比较
false>=1
。紧贴原始代码,您可以将比较包装到
开始
/
结束
块中

loop do
  puts "How many soaps are you looking to purchase? (Please put an integer)"
  x = gets.chomp
  begin
    barsofsoap = Integer(x)
    if barsofsoap >= 1
      break
    end
  rescue ArgumentError => _error
    puts "You didn't put an integer, please put an integer and try again."
  end
end
或者简化代码:

loop do
  puts "How many soaps are you looking to purchase? (Please put an integer)"
  break if (barsofsoap = gets.to_i).positive?
  puts "Invalid input. Provide a positive integer."
end
上面的代码无法检测输入是字符串还是整数。通过将
Integer(x)
替换为
x.to_i
如果无效,它将简单地返回
0
。因为您希望用户提供一个正整数,所以它仍然要求用户提供一个新整数

nil.to_i #=> 0
''.to_i #=> 0
'asdf'.to_i #=> 0
'12.34'.to_i #=> 12
如上图所示,字符串
'12.34'
将生成值
12
。根据您的要求,您可能不希望使用此值,而是将其标记为无效输入。在这种情况下,可以使用正则表达式检查提供的输入

loop do
  puts "How many soaps are you looking to purchase? (Please put an integer)"
  input = gets
  break barsofsoap = input.to_i if input.match?(/\A[1-9]\d*\Z/)
  puts "Invalid input. Provide a positive integer."
end
正则表达式
/\A[1-9]\d*\Z/
将匹配以下内容:

  • \A
    匹配字符串的开头
  • [1-9]
    匹配其中一个数字
    1-9
  • \d*
    匹配零个或多个数字
    0-9
  • \Z
    匹配字符串的结尾。如果字符串以换行符结尾,它将在换行符之前匹配

其他选项,使用
n.to_i.to_s==n
检查输入是否为整数,并将循环移动到方法中:

def get_input_integer
  loop do
    puts "Enter a positive Integer"
    n = gets.chomp
    if n.to_i.to_s == n
      return n.to_i if n.to_i.positive?
      puts "It's not positive"
    else
      puts "It's not an integer" 
    end
  end
end

x = get_input_integer

如果barsofsoap>=1
实际上是正确的检查方法
match
适用于字符串类型,但您的
barsofsoap=Integer(x)
确保您处理的是数字类型。再试一次@Chris Heald如果barsofsoap>=1起作用,但是如果我输入一个字符串,程序就会中断,我想我的问题是如果我输入一个字符串,如何使程序不中断。这是可行的,但是如果我输入一个字符串,程序就会中断,我试图找到一种方法,即使我输入一个字符串,程序也不会中断。为什么在这里检查两次<代码>如果barsofsoap>=0&&barsofsoap.positive?@Zach-Tuttle感谢您的回答。我以前试过这个方法,但是如果我输入一个十进制数,程序会认为它是一个整数并继续运行,我不希望用户输入一个浮点。@Leslie只需调用#floor方法将十进制数一起删除即可。请参阅更新的示例。@Zach Tuttle我试过了。当我输入一个十进制数时,程序仍将继续运行。
方法更为惯用,因为响应
的任何对象都是有效的(鸭子式键入),而不是强制输入
整数并希望它成功。@Leslie我已将答案更新为只允许数字1到4。查看新的代码块。@3limin4t0r抱歉,这是一个输入错误,我希望数字是1到无穷大