Home  >  Article  >  Java  >  How to determine the number of palindromes in java

How to determine the number of palindromes in java

王林
王林forward
2020-09-03 16:14:274800browse

How to determine the number of palindromes in java

First of all, let’s introduce what is a palindrome number?

(Recommended tutorial: java introductory tutorial)

is the number 12321, 11111, 63936. If you read it forward and backward, it is the same number. It is called Palindrome number.

Two operations: / and %

/: division operation. It should be noted here that if two integers get along with each other, the result will also be an integer. For example, 3/2=1. 2/3=0.

%: Touch: Remainder operation. For example, 1%3=1. 13 =3.

The idea of ​​​​judging palindrome numbers:

One way of thinking is this: just invert the number (123 becomes 321) and then determine whether the two are equal.

(Related recommendations: java course)

Implementation code:

public class Test{
   public static void main(String[]args){
   System.out.println("请输入一个数字");
   Scanner reader = new Scanner(System.in);
   int num = reader.nextInt();
   Judge (num);
   }
   private static void Judge(int num){
   int num2=0;//这个数用来存储倒置后的数字
   int num3=num;//因为以后会用到 num num的数会改变所以再用一个变量记录一下num
   //下面我们开始我们的循环
   while(num>0){
   num2 = num2*10+num%10;
   num = num/10;		
      }
     if (num2 == num3) {//若倒置后的数字与原先的数字相等则说明为回文数
   System.out.println("该数是回文数");
       }
  else {
   System.out.println("该不是回文数");
      }
   }
}

The above is the detailed content of How to determine the number of palindromes in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete