如何显示所有回文和所有主要回文

时间:2015-05-01 12:15:58

标签: java arrays palindrome

好的,所以我差不多完成了这个程序,但是我需要它来显示从100到“用户输入”的所有回文以及该列表中的所有主要回文。到目前为止,我只能让它显示所有主要的回文。

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
System.out.print("Please enter a number greater than 100: ");
int userInput = input.nextInt();

    if(userInput <= 100)
    {
        System.out.print("That number is not big enough, please enter a "
                + "number greater than 100");
    }
    else
    {
        System.out.println("The Prime Palindrome Integer between 101"
                + " and " + userInput + " are: ");
    }

for (int i = 101; i <= userInput; i++)
    {
        Integer userInt = new Integer(i);
        String userString = userInt.toString();
        char[] userChar = userString.toCharArray();

        if (isPrime(i) && arraysAreEqual(userChar, reverseArray(userChar)))
        {
            printArray(userChar);
            System.out.print(", ");
        }
    }
}

public static char[] reverseArray(char[] list)
{
    String reverse = new StringBuilder(new String(list)).reverse().toString();

char[] revArray = reverse.toCharArray();
    return revArray;
}

public static boolean arraysAreEqual(char[] list1, char[] list2)
{
    if (Arrays.equals(list1, list2) )
    return true;
    else return false;
}

public static boolean isPrime(int intPrime)
{
for (int i = 2; i < Math.sqrt(intPrime); i++)
    {
        if (intPrime % i == 0)        
        return false;
    }
return true;
}

public static void printArray(char[] list)
{
for (int i = 0; i < list.length ; i++)
    {
        System.out.print(list[i]);
}
}

}

2 个答案:

答案 0 :(得分:0)

好的,所以这里有一些代码更改:

首先,你需要改变你的循环,所以元素被添加到两个列表中,一个带有素数,另一个没有质数。

sc._scj.sc()

然后,快速更改 List<Integer> primes = new ArrayList<>(), nonPrimes = new ArrayList<>(); for (int i = 101; i <= userInput; i++) { Integer userInt = new Integer(i); String userString = userInt.toString(); char[] userChar = userString.toCharArray(); if (arraysAreEqual(userChar, reverseArray(userChar))) { nonPrimes.add(i); if(isPrime(i)) { primes.add(i); } } } printArray(nonPrimes); System.out.println(); //Added to include a new line printArray(primes); 方法:

printArray()

就是这样。

答案 1 :(得分:0)

从你的问题来看,很明显你需要分别储存回文和主要的回文。所以我的建议你可以声明2 ArrayList个变量说palindromeprimePalindrome像这样:

  ArrayList<String> palindrome = new ArrayList<>();
  ArrayList<String> primePalindrome = new ArrayList<>();

然后替换此if

    if (isPrime(i) && arraysAreEqual(userChar, reverseArray(userChar)))
    {
        printArray(userChar);
        System.out.print(", ");
    }

使用:

    if (arraysAreEqual(userChar, reverseArray(userChar))){
       if(isPrime(i)){
          primePalindrome.add(userString);
       }else{
          palindraome.add(userString);
       }
    }

最后删除函数调用printArray(),您可以编写此代码以在for循环后打印输出。

System.out.println("The Non prime palindromes are :");
for(String str:palindrome){
   System.out.println(str);
}


System.out.println("The prime palindromes are :");
for(String str:primePalindrome){
   System.out.println(str);
}

完成可运行代码 我发布这个完整的可运行代码只是因为你说你无法理解我在说什么。我已经实现了上面提到的所有内容。运行它,你会得到你的结果。

import java.util.*;
import java.io.*;

public class test1{
    static ArrayList<String> palindrome = new ArrayList<>();
    static ArrayList<String> primePalindrome = new ArrayList<>();
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a number greater than 100: ");
        int userInput = input.nextInt();

        if(userInput <= 100){
            System.out.print("That number is not big enough, please enter a "
                    + "number greater than 100");
        }else{
            System.out.println("The Prime Palindrome Integer between 101"
                    + " and " + userInput + " are: ");
        }

        for (int i = 101; i <= userInput; i++){
            Integer userInt = new Integer(i);
            String userString = userInt.toString();
            char[] userChar = userString.toCharArray();
            if (arraysAreEqual(userChar, reverseArray(userChar))){
                if(isPrime(i)){
                    primePalindrome.add(userString);
                }else{
                    palindrome.add(userString);
                }
            }
        }

        System.out.println("The Non prime palindromes are :");
        for(String str : palindrome){
            System.out.println(str);
        }


        System.out.println("The prime palindromes are :");
        for(String str : primePalindrome){
            System.out.println(str);
        }
    }

    public static char[] reverseArray(char[] list){
        String reverse = new StringBuilder(new String(list)).reverse().toString();

        char[] revArray = reverse.toCharArray();
        return revArray;
    }

    public static boolean arraysAreEqual(char[] list1, char[] list2){
        if (Arrays.equals(list1, list2) )
            return true;
        else return false;
    }

    public static boolean isPrime(int intPrime){
        for (int i = 2; i < Math.sqrt(intPrime); i++){
            if (intPrime % i == 0)        
                return false;
        }
        return true;
    }

}