实验二 (二) 判断回文数

(2)编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、3223都是回文数。

 

如何从键盘输入一个数呢?

 1 package test;
 2 import java.util.Scanner;
 3 public class KeyboardInput {
 4     public static void main(String[] args){
 5         Scanner x = new Scanner(System.in);//构造一个Scanner对象,其传入参数为System.in 
 6         System.out.println("请输入一个整数");
 7         int i = x.nextInt();//读取一个int数值
 8         System.out.println("你输入的数字是:"+i);
 9     }
10 }

 

 1 package hi;
 2 import java.util.Scanner;
 3 public class Palindrome {
 4     public static void main(String[] args){
 5         Scanner x = new Scanner(System.in);//构造一个Scanner对象,其传入参数为System.in 
 6         System.out.println("请输入一个整数");
 7         int num= x.nextInt();//读取一个int数值
 8         int a1,a2,a3,a4,a5;
 9         a5=num/10000;
10         a4=num%10000/1000;
11         a3=num%1000/100;
12         a2=num%100/10;
13         a1=num%10;
14         if(a5!=0){
15             System.out.println(num+" is 5 digits");
16             if(a5==a1&&a2==a4){
17                 System.out.println(num+" is a palindrome");
18             }else{
19                 System.out.println(num+" is not a palindrome");
20             }
21         }
22         else if(a4!=0){
23             System.out.println(num+" is 4 digits");
24             if(a1==a4&&a2==a3){
25                 System.out.println(num+" is a palindrome");
26             }else{
27                 System.out.println(num+" is not a palindrome");
28             }
29         }
30         else if(a3!=0){
31             System.out.println(num+" is 3 digits");
32             if(a1==a3){
33                 System.out.println(num+" is a palindrome");
34             }else{
35                 System.out.println(num+" is not a palindrome");
36             }
37             
38         }
39         else if(a2!=0){
40             System.out.println(num+" is 2 digits");
41             if(a1==a2){
42                 System.out.println(num+" is a palindrom");
43                 
44             }
45             else{
46                 System.out.println(num+" is not a palindrom");
47             }
48                 
49         }
50         else if(a1!=0){
51             System.out.println(num+" is 1 digits");
52             System.out.println(num+" is a palindrom");
53         }
54         else{
55             System.out.println(num+" is not between 0 and 99999");
56         }
57              
58     }
59 }

 

转载于:https://www.cnblogs.com/CheeseIce/p/10503156.html

你可能感兴趣的:(实验二 (二) 判断回文数)