先求出整数的反向倒置数,然后进行比较(不是通过将数字转换成字符串的方法)
代码:
package com.im;
import java.util.Scanner;
public class Demo63 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a number (0, 100000000):");
int number = input.nextInt();
if(number>0 && number<100000000){
System.out.println(isPalindrome(number));
}else{
System.out.println("输入超出范围!");
System.exit(0);
}
}
public static boolean isPalindrome(int number){
if(number == reverse(number)){
return true;
}else{
return false;
}
}
public static int reverse(int number){ //输入数的反向倒置数
int v = 0; //整数的各位字上的数字
int m = 0; //整数的反向倒置数
int count = 0;
count = digitOfNumber(number);
for(int i=1; i<=count; i++){
v = number%10;
m += v*Math.pow(10, count-i);
number /= 10;
}
return m;
}
public static int digitOfNumber(int number){ //获取整数有多少位数
int count = 0;
while(number>0){
number = number/10;
count++;
}
return count;
}
}