Given an input string of a signed 32-bit integer x from the console, print out x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31-1], then print out 0.
输入一个32位以内的string类值,输出它的相反值,如果相反值超出范围,则输出0
Example#1:
Input:
123
Output:
321
Example#2:
Input:
-256
Output:
-652
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long input = sc.nextLong();//一定要用long,int的位数太少了,位数多了的话会报错
if(input>=Integer.MAX_VALUE||input<=Integer.MIN_VALUE)//判断是否高于整型变量的最大值
{
System.out.print("0");
return;
}
if(input>=0)/此时是正数
{
while(input!=0)
{
long reverse =0;
reverse=input%10;
input = input/10;
System.out.print(reverse);
}
}
else//此时是负数
{
long abinput=Math.abs(input);
System.out.print("-");
while(abinput!=0)
{
long reverse =0;
reverse=abinput%10;
abinput = abinput/10;
System.out.print(reverse);
}
}
}
}