这个很经典的,基本上基础部分都会有这样的小练习
代码如下:
package ClassDemo; public class PrimePalTest {
public static void main (String[] args) {
int rows = 10;
int maxPrimePalNumber = 100;
for (int num = 2, count = 0; count < maxPrimePalNumber; num++) {
if (isPalindrome(String.valueOf(num)) && isPrime(num)) {
if ((count + 1) % rows != 0) {
System.out.print(" " + num);
} else {
System.out.println(" " + num);
}
count++;
}
}
}
private static boolean isPrime(int number) {
for (int divisor = 2; divisor <= number / 2; divisor ++) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
private static boolean isPalindrome(String s) {
int low = 0;
int high = s.length()-1;
boolean flag = true;
while (low < high) {
if(s.charAt(low) != s.charAt(high)) {
flag = false;
break;
}
low++;
high--;
}
return flag;
}
}