題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。
1 package com.li.FiftyAlgorthm; 2 3 import java.util.Scanner; 4 5 /** 6 * 題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。 7 * @author yejin 8 */ 9 public class Palindrom { 10 public static void main(String[] args) { 11 Scanner s = new Scanner(System.in); 12 System.out.print("請輸入一個正整數:"); 13 long a = s.nextLong(); 14 String ss = Long.toString(a); 15 char[] ch = ss.toCharArray(); 16 boolean is = true; 17 int j = ch.length; 18 for (int i = 0; i < j / 2; i++) { 19 if (ch[i] != ch[j - i - 1]) { 20 is = false; 21 } 22 } 23 if (is == true) { 24 System.out.println("這是一個回文數"); 25 } else { 26 System.out.println("這不是一個回文數"); 27 } 28 } 29 }