-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; 
    }
}

Currently the thing compiles and ask for input, but it always results in nothing.

Does someone see something wrong with my program that is obvious wrong.

Overall, the program receives input and has to look through values 2 until it hits the value the user wants and print the ones that follow the if statement.

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 に答える