Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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
Java 在int限制内反转整数值_Java - Fatal编程技术网

Java 在int限制内反转整数值

Java 在int限制内反转整数值,java,Java,我想反转一个整数。我已经尝试了下面的代码,当数字太大以至于其反向大于int限制(例如1534236469)时,就会出现问题,因此在这种情况下,它会返回一些垃圾整数 如何使用Integer.MIN\u VALUE和Integer.MAX\u VALUE检查反转的数字是否在限制范围内 注意:它只能使用int变量类型 int num = 1534236469; int reverseInt = 0; int multiplier = 1; if ( num < 0 ) { multi

我想反转一个整数。我已经尝试了下面的代码,当数字太大以至于其反向大于int限制(例如1534236469)时,就会出现问题,因此在这种情况下,它会返回一些垃圾整数

如何使用
Integer.MIN\u VALUE
Integer.MAX\u VALUE
检查反转的数字是否在限制范围内

注意:它只能使用
int
变量类型

int num = 1534236469;

int reverseInt = 0;
int multiplier = 1;

if ( num < 0 ) {
    multiplier *= -1;
}

while ( num != 0 ) {

    //get the last digit
    int digit = num % 10;

    //multiply the reverseInt by 10 and then add the last digit
    reverseInt = (reverseInt * multiplier) + digit;

    multiplier = 10;
    num /= 10;
}

//how to fix this.
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
    System.out.println("Invalid number");
} else {
    System.out.println("Reversed integer is " + reverseInt);
        }
int num=1534236469;
int reverseInt=0;
整数乘数=1;
if(num<0){
乘数*=-1;
}
while(num!=0){
//得到最后一位数
整数位数=num%10;
//将倒数乘以10,然后将最后一位数字相加
反向输入=(反向输入*乘数)+数字;
乘数=10;
num/=10;
}
//如何解决这个问题。
if(reverseIntInteger.MAX_值){
System.out.println(“无效编号”);
}否则{
System.out.println(“反转整数为”+反转整数);
}
在Java 8+中,更改为:

reverseInt = Math.addExact(Math.multiplyExact(reverseInt, multiplier), digit);

如果结果溢出,代码现在将抛出算术异常。

每当超过
Integer.MAX\u value
的值时,它将从
Integer.MIN\u value
开始,即
Integer.MAX\u value+1
计算为
Integer.MIN\u value
。您可以使用此功能获得您正在寻找的解决方案

在从
1
10
的循环中,只需将
reverseInt
相乘,如果该值变为负值,则意味着下一个值(
reverseInt*乘数
)将超过
整数.MAX_值
。此外,您需要在循环中将
reverseInt*乘数
添加到从
0
9
的值,如果该值变为负值,则意味着下一个值(
reverseInt*乘数+数字
)将超过
整数。最大值

public class Main {
    public static void main(String[] args) throws InterruptedException {
        int num = 1534236469;
        boolean valid = true;
        int reverseInt = 0;
        int multiplier = 1;

        if (num < 0) {
            multiplier *= -1;
        }

        while (num != 0) {
            // get the last digit
            int digit = num % 10;

            for (int i = 1; i <= multiplier; i++) {
                for (int j = 0; j <= 9; j++) {
                    if (reverseInt * i < 0 || (reverseInt * i + j) < 0) {
                        System.out.println("Invalid number");
                        valid = false;
                        break;
                    }
                }
                if (!valid) {
                    break;
                }
            }
            if (!valid) {
                break;
            }

            // multiply the reverseInt by 10 and then add the last digit
            reverseInt = reverseInt * multiplier + digit;

            multiplier = 10;
            num /= 10;
        }
        if (valid) {
            System.out.println("Reversed integer is " + reverseInt);
        }
    }
}

您只需在添加下一个数字之前进行检查,这样您就可以简单地将检查移动到实际计算。如果当前反向正数大于整数.MAX_VALUE/10,则不能再添加其他数字。负数也是如此

我所做的唯一一件事就是将代码的这一部分向上移动:

if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
    System.out.println("Invalid number");
} else {

或者,您可以将其视为字符串,然后简单地反转字符串(同时摆弄符号):


你必须使用
Integer.MIN\u VALUE
Integer.MAX\u VALUE
吗?@Scratte不,我就是这么想的。
if (reverseInt < Integer.MIN_VALUE || reverseInt > Integer.MAX_VALUE ) {
    System.out.println("Invalid number");
} else {
public class StackOverflowTest {
  public static void main(String[] args) {
    int num = -1534236469;

    int reverseInt = 0;
    int multiplier = 1;

    if ( num < 0 ) {
        multiplier *= -1;
    }

    while ( num != 0 ) {

        // if this next step will push is into overflow, then stop:
        if (reverseInt < Integer.MIN_VALUE/10 || reverseInt > Integer.MAX_VALUE/10) {
          System.out.println("Invalid number");
          return;

        } else {

          //get the last digit
          int digit = num % 10;

          // multiply the reverseInt by 10 and then add the last digit
          reverseInt = (reverseInt * multiplier) + digit;
        }

        multiplier = 10;
        num /= 10;
    }

    System.out.println("Reversed integer is " + reverseInt);
  }
}
public class StackOverflowTest {
  public static void main(String[] args) {

    reverse(1534236469);
    System.out.println();
    reverse(-153423646);
  }

  public static void reverse(int num) { 
    System.out.println("int = " + num);
    int number = num < 0 ? -num : num;           // remove the sign

    String reverse = new StringBuilder(String.valueOf(number)).reverse().toString();
    System.out.println("reverse String: " + reverse);

    try {
      int reversed = Integer.parseInt(reverse);
      reversed = num < 0 ? -reversed : reversed;  // get back the sign
      System.out.println("Reversed integer is " + reversed);
    } catch (NumberFormatException ex) {
      System.out.println("Invalid number");
    }
  }
}
int = 1534236469
reverse String: 9646324351
Invalid number

int = -153423646
reverse String: 646324351
Reversed integer is -646324351