java determines whether it is a palindrome number
The following introduces a simple judgment method, which has been explained in the specific implementation code:
import java.util.*; public class StringBufferDemo { public static void main(String[] args) { // TODO Auto-generated method stub //从键盘上输入一个字符串str String str = ""; System.out.println("请输入一个字符串:"); Scanner in = new Scanner(System.in); str = in .nextLine(); //根据字符串创建一个字符缓存类对象sb StringBuffer sb = new StringBuffer(str); //将字符缓存中的内容倒置 sb.reverse(); //计算出str与sb中对应位置字符相同的个数n int n = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == sb.charAt(i)) n++; } //如果所有字符都相等,即n的值等于str的长度,则str就是回文。 if (n == str.length()) System.out.println(str + "是回文!"); else System.out.println(str + "不是回文!"); } }
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of Java determines whether it is a palindrome number. For more information, please follow other related articles on the PHP Chinese website!