-1
import java.util.Scanner; 

public class PD {
    public static void main(String[] args) { 

        Scanner input = new Scanner (System.in);    

        System.out.print("Enter your number: " ); 
        int number = input.nextInt();
        for (int count = 2; count < number; count++) {
            String blank = "";
            String Snumber = count + blank; 
            if (isPalindromic(count) && isPrime(count) && 
                isPalindromic((int)(Snumber.length())) && 
                isPrime((int)(Snumber.length()))){
                    System.out.println( count + "is double palidromic prime");
            }
            else
                continue; 
        }
    }

    // method to find palindromic 
    public static boolean isPalindromic(int count) {
        String blank = ""; 
        String convert = count + blank; 
        for (int i = 0, q = 1; i <= (convert.length()/2 - 1); i++, q++) { 
            if (convert.substring(i,q) == convert.substring(convert.length() - q, convert.length() - i)){
                return true;
            }
        }
        return false; 
    }

    // method to find prime
    public static boolean isPrime(int count ) {
        for (int divisor = 2; divisor <= count/2; divisor++) { 
            if (count % divisor == 0) { 
                return false; 
            }
        }
        return true; 
    }
}

目前这个东西编译并要求输入,但它总是没有结果。

有人看到我的程序有什么明显错误的地方吗?

总的来说,程序接收输入并且必须查看值 2 直到它达到用户想要的值并打印 if 语句后面的值。

4

1 回答 1

2

您的isPalindromic方法无法正常运行。它不会返回true回文数。将其更改为:

public static boolean isPalindromic(int count) {
        String blank = "";
        String convert = count + blank;
        int n = convert.length();
        for (int i = 0; i < (n / 2 + 1); i++) {
            if (convert.charAt(i) != convert.charAt(n - i - 1)) {
                return false;
            }
        }

        return true;
}
于 2013-10-31T02:49:49.530 回答