java例题_25 判断是否为回文数!

 1 /*25 【程序 25 求回文数】 
 2 题目:一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位相同,十位与千位相同。
 3 */
 4 
 5 /*分析
 6  * 先用%和/将5个数字分离,再组成一个新的5位数,如果这个新的5位数与原数相等,则输出yes,否者no
 7  * */
 8 
 9 
10 package homework;
11 
12 import java.util.InputMismatchException;
13 import java.util.Scanner;
14 
15 public class _25 {
16 
17     public static void main(String[] args) {
18         int x=0;
19         // 从键盘得到一个5位数正整数
20         while (true) {
21             System.out.println("请输入一个5位正整数:");
22             try {
23                 // 从键盘得到一个正整数
24                 Scanner sc = new Scanner(System.in);
25                 x = sc.nextInt();
26                 if ((x >=10000) & (x <= 99999)) // 判断是否为5位数以内的正整数
27                     break;
28             } catch (InputMismatchException e) { // 捕获输入异常
29                 System.out.println("输入错误:" + e.toString());
30             }
31 
32         }
33         //声明n1,n2,分别表示原数和新生成的5位数
34         int n1=x,n2=0;
35         while(x>0) {
36             n2=n2*10+(x%10);
37             x=x/10;
38 //            System.out.println("x:"+x+"\t"+"n2:"+n2);   //测试
39         }
40         if(n1==n2) {
41             System.out.println("yes!");
42         }
43         else {
44             System.out.println("no!");
45         }
46 
47     }
48 
49 }

猜你喜欢

转载自www.cnblogs.com/scwyqin/p/12313263.html