Java 考虑边界条件的数的倒数

Java 考虑边界条件的数的倒数,java,integer-overflow,boundary,Java,Integer Overflow,Boundary,我试图用Java编写一个简单的方法,返回数字的倒数(以数学方式,而不是字符串方式)。我想考虑边界条件,因为一个倒数超出int范围的数字会给我一个错误的答案。即使抛出异常,我也没有得到清晰的逻辑。我试过这个代码 private static int reverseNumber(int number) { int remainder = 0, sum = 0; // One could use comma separated declaration for primitives and

我试图用Java编写一个简单的方法,返回数字的倒数(以数学方式,而不是字符串方式)。我想考虑边界条件,因为一个倒数超出int范围的数字会给我一个错误的答案。即使抛出异常,我也没有得到清晰的逻辑。我试过这个代码

private static int reverseNumber(int number) {
int remainder = 0, sum = 0; // One could use comma separated declaration for primitives and
                            // immutable objects, but not for Classes or mutable objects because
                            // then, they will allrefer to the same element.
boolean isNegative = number < 0 ? true : false;
if (isNegative)
  number = Math.abs(number); // doesn't work for Int.MIN_VALUE
                             // http://stackoverflow.com/questions/5444611/math-abs-returns-wrong-value-for-integer-min-value

System.out.println(number);
while (number > 0) {
  remainder = number % 10;
  sum = sum * 10 + remainder;
  /* Never works, because int won't throw error for outside int limit, it just wraps around */
  if (sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE) {
    throw new RuntimeException("Over or under the limit");
  }
  /* end */
  /* This doesn't work always either.
   * For eg. let's take a hypothetical 5 bit machine.
   * If we want to reverse 19, 91 will be the sum and it is (in a 5 bit machine), 27, valid again!
   */
  if (sum < 0) {
    throw new RuntimeException("Over or under the limit");
  }

  number /= 10;
}
return isNegative ? -sum : sum;
private static int reverseEnumber(整数编号){
int rements=0,sum=0;//可以对原语和
//不可变对象,但不适用于类或可变对象,因为
//然后,它们都将引用相同的元素。
布尔值isNegative=数值<0?真:假;
如果(为负)
number=Math.abs(number);//不适用于Int.MIN_值
// http://stackoverflow.com/questions/5444611/math-abs-returns-wrong-value-for-integer-min-value
系统输出打印项次(编号);
而(数量>0){
余数=数字%10;
总和=总和*10+余数;
/*永远不会工作,因为int不会抛出超出int限制的错误,它只是绕过去*/
如果(总和>整数.MAX|u值|总和<整数.MIN_值){
抛出新的RuntimeException(“超过或低于限制”);
}
/*结束*/
/*这也不总是有效的。
让我们假设一个5位的机器。
*如果我们想反转19,91将是总和,它是(在5位机器中),27,再次有效!
*/
如果(总和<0){
抛出新的RuntimeException(“超过或低于限制”);
}
数目/=10;
}
返回值为负?-总和:总和;

}

你的方法是除以10,将提醒转换为当前结果*10

你唯一做错的事情就是检查“违反边界”,因为

不应超过整数值。最大值,即

                 (!)
Integer.MAX_VALUE >= sum * 10 + remainder;
或改造:

                                    (!)
(Integer.MAX_VALUE - remainder) / 10 >= sum
因此,在乘以10并添加余数之前,可以使用以下检查:

while (number > 0) {
  remainder = number % 10;

  if (!(sum <= ((Integer.MAX_VALUE -remainder) / 10))) {
    //next *10 + remainder will exceed the boundaries of Integer.
    throw new RuntimeException("Over or under the limit");
  }

  sum = sum * 10 + remainder;
  number /= 10;
}
这非常有意义——因为这正是下一步的反向计算——如果总和已经大于此计算——下一步将超过
Integer.MAX_值


未经测试,但这应该可以解决问题。

您可以使用
long
s,并根据需要比较它们是否适合
int
范围。请提供一些输入示例和预期输出<代码>以数学的方式,而不是字符串的方式,似乎有点含糊不清。@dognose我的意思是我不想将其转换为字符串,然后再将其反转。@Srivatskrishnan好的,所以“9876”仍然应该变成“6789”-但是使用“计算”,而不是字符串操作?@dognose是的。
                                    (!)
(Integer.MAX_VALUE - remainder) / 10 >= sum
while (number > 0) {
  remainder = number % 10;

  if (!(sum <= ((Integer.MAX_VALUE -remainder) / 10))) {
    //next *10 + remainder will exceed the boundaries of Integer.
    throw new RuntimeException("Over or under the limit");
  }

  sum = sum * 10 + remainder;
  number /= 10;
}
if (sum > ((Integer.MAX_VALUE -remainder) / 10))