回文整数是指正读和反读相同的整数,输入一个整数,判断其是否为回文整数。
1: // 回文整数
2: import java.io.*;
3: public class t2_14{
4: public static void main(String argv[]){
5: String str = null;
6: System.out.println("Please input your number : ");
7: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
8: try {
9: str = in.readLine();
10: } catch (IOException e) {
11: // TODO 自动生成的 catch 块
12: e.printStackTrace();
13: }
14: if(str.isEmpty()){
15: System.out.println("Empty ...");
16: System.exit(5);
17: }
18: str = str.trim();
19: if(str.startsWith("-"))
20: str = str.substring(1);
21: if(str.length()%2!=0){
22: char m = str.charAt(str.length()/2);
23: if(m<'0' || m>'9'){
24: System.out.println("It's not a number !");
25: System.exit(4);
26: }
27: }
28: int length = str.length()/2;
29: for(int i=0;i<length;i++){
30: char head = str.charAt(i);
31: if(head<'0' || head>'9'){
32: System.out.println("It's not a number !");
33: System.exit(1);
34: }
35: char tail = str.charAt(str.length()-i-1);
36: if(tail<'0' || tail>'9'){
37: System.out.println("It's not a number !");
38: System.exit(2);
39: }
40: if(head!=tail){
41: System.out.println("It's not a right number !");
42: System.exit(3);
43: }
44: }
45: System.out.println("It's right number !");
46: }
47: }