Home > Java > javaTutorial > body text

How to determine whether a number is a palindrome in java

silencement
Release: 2019-06-21 14:06:31
Original
6274 people have browsed it

There is a type of numbers that look the same when viewed backwards and forwards, for example: 121, 656, 2332, etc. Such numbers are called palindrome numbers.

How to determine whether a number is a palindrome in java

#Write a function to determine whether a number is a palindrome number.

public class PalindromeNumber {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数字:");
        String strNum = scan.next();
        boolean result = isPalindrome(strNum);
        System.out.println(result);
    }
    
    public static Boolean isPalindrome(String str){
        boolean result=false;
        for(int i=0; i<str.length()/2;i++){
            if(str.charAt(i) == str.charAt(str.length()-1-i)){
                result=true;
            }else{
                return result;
            }
        }
        return result;
    }
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template