JAVA實例:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。
import java.util.Scanner; publicclass Ex25 { staticint[] a = newint[5]; staticint[] b = newint[5]; publicstaticvoid main(String[] args) { boolean is =false; Scanner s = new Scanner(System.in); long l = s.nextLong(); if (l > 99999 || l < 10000) { System.out.println("Input error, please input again!"); l = s.nextLong(); } for (int i = 4; i >= 0; i--) { a[i] = (int) (l / (long) Math.pow(10, i)); l =(l % ( long) Math.pow(10, i)); } System.out.println(); for(int i=0,j=0; i<5; i++, j++) { b[j] = a[i]; } for(int i=0,j=4; i<5; i++, j--) { if(a[i] != b[j]) { is = false; break; } else { is = true; } } if(is == false) { System.out.println("is not a Palindrom!"); } elseif(is == true) { System.out.println("is a Palindrom!"); } } } *