Java判断回文数

package com.stx.test;

import java.util.Scanner;

public class 回文数 {
	public static void main(String[] args) {
		String str = "";
		System.out.println("请输入:");
		Scanner input = new Scanner(System.in);
		str = input.next();
		StringBuffer sb = new StringBuffer(str);
		sb.reverse();//将字符反转
		int count = 0;
		for (int i = 0; i < str.length(); i++) {//每位对比
			if (str.charAt(i) == sb.charAt(i)) {
				count++;
			}

		}
		if (str.length() == count) {//相同的位数与长度做对比
			System.out.println(str + "是回文数");
		} else {
			System.out.println(str + "不事回文数");
		}
	}

}

 

你可能感兴趣的:(JAVA)