簡體   English   中英

檢查數組中的字符以建立回文列表

[英]Checking characters in an array to build palindrome list

我需要制作一個數組,並用25個5位回文數填充。 我為先前的分配創建了一種方法,該方法使用for循環將其反向輸出。 對於這個,我想以數組的形式做所有事情,以學習新東西。 我發現這個代碼 ,我可以使生成00000 -通過增加兩個字符,並且使所有的范圍為0 99999 - 9 后見底了完整的代碼。

char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

為了清楚起見,在main()中使用了digit1,如下所示:

char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit3 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit4 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] digit5 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

char[][] arrs = {digit1, digit2, digit3, digit4, digit5};
int[] inds = new int[5];

我嘗試將printIndices方法的輸出部分轉換為String,以為可以使用charAt或StringBuilder.reverse()。 現在我有這個:

String output = String.valueOf(arrs[i][inds[i]]);
System.out.print(output);

哪個工作正常,但如果我嘗試這樣做:

String palindrome = new StringBuilder(output).reverse().toString();
System.out.print(palindrome); 

我得到了與以前完全相同的字符-我是否正確地認為,如果使用該方法反轉00001,結果將為10000?

如果我使用charAt:

char output1 = output.charAt(1);
System.out.print(output1);

對於charAt(0)以外的任何其他值,我都會收到以下錯誤:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:646)
at PalindromeArray.printIndices(PalindromeArray.java:31)
at PalindromeArray.main(PalindromeArray.java:51)
Java Result: 1

如果我使用charAt(0),它將打印所有數字,這使我認為它們沒有被視為可以分開的單個字符,因此,如果我嘗試訪問第一個字符以外的內容,則會出現錯誤。

public class PalindromeArray

{
    static char[] digit1 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit2 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit3 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit4 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    static char[] digit5 = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

static boolean incrementIndices(char[][] arrs, int[] inds) 
{
int n = arrs.length;
for(int i = n - 1; i >= 0; i--) 
{
    if(inds[i] < arrs[i].length - 1) 
    {
        inds[i]++;
        return true;
    }
    inds[i] = 0;
}
return false; // could not increment further
}

static void printIndices(char[][] arrs, int[] inds) 
{
int n = arrs.length;
for(int i = 0; i < n; i++) 
{

    String output = String.valueOf(arrs[i][inds[i]]);

    for(char c1: digit1)
{
    for(char c2: digit1)
    {
        for(char c3: digit1)
        {
            for(char c4: digit1)
            {
                for(char c5: digit1)
                {
                    String tempCheck = ""+c1+c2+c3+c4+c5;
                    if(isPalindrome(tempCheck))
                    {
                        System.out.println(tempCheck);
                    }
                }
            }
        }
    }
}

    System.out.print(output);


}
System.out.println();
}

public static boolean isPalindrome(String str)
{
String opposite = "";
for(int a = str.length()-1; a > 0; a++)
{
    opposite += str.charAt(a);
}
return opposite.equals(str); 
}

public static void main(String[] args)
{


    char[][] arrs = {digit1, digit2, digit3, digit4, digit5};
    int[] inds = new int[5];

    do 
    {
        printIndices(arrs, inds);
    } 
    while(incrementIndices(arrs, inds));
 }

}
/*
Project Name: PalindromeLab
Programmer: Jack
Class: Computer Science
Date: 10/14/16
Description: Simple Palindrome Programm
*/
import java.util.Scanner;

public class NewPalindromeLab
{ 
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.println("Welcome to Palindrome Converter");
      System.out.print("Enter in what you want Palindromeinated ==> ");
      String palin = input.nextLine();
      System.out.println("The origional was: " + palin);

      palin = palin.toLowerCase();

      int length = palin.length();

      char[] tempCharArray = new char[length];
      char[] charArray = new char[length];

      for(int i=0; i<length; i++)
      {
         tempCharArray[i] = palin.charAt(i);
      } 

      for(int k=0; k<length; k++)
      {
         charArray[k] = tempCharArray[length - 1 - k];
      }

      String reversePalin = new String (charArray);
      String reverseNewPalin = "";
      reverseNewPalin = reversePalin.replaceAll("[^A-Za-z0-9]", "");

      System.out.println("The reversed word is: " + reverseNewPalin);

      if (reversePalin.equals(palin))
      {
         System.out.println("This is a palindrome!");
      }
      else      
      {
         System.out.println("This is not a palindrome!");
      }
   }
}

/*
The following is the Output of the program:
Welcome to Palindrome Converter
Enter in what you want Palindromeinated ==>  Hello
The origional was:  Hello
The reversed word is: olleh
This is not a palindrome!
*/

您總是可以創建如下方法

public static boolean isPalindrome(String str)
{
    String opposite = "";
    for(int a = str.length()-1;a>0;a++)
    {
        opposite+=str.charAt(a);
    }
    return opposite.equals(str);
}

編輯:我將編寫實現它的方法。

    for(char c1: digit1)
    {
        for(char c2: digit1)
        {
            for(char c3: digit1)
            {
                for(char c4: digit1)
                {
                    for(char c5: digit1)
                    {
                        String tempCheck = ""+c1+c2+c3+c4+c5;
                        if(isPalindrome(tempCheck))
                        {
                            System.out.println(tempCheck);
                        }
                    }
                }
            }
        }
    }

我最終完全轉儲了代碼並走了另一條路。 從有人進行相同練習的Java論壇中借來的一部分,使用隨機數生成器對其進行了改進,使整數范圍可以滿足該項目的需要,並增加了回文總數的計數器。

感謝所有做出貢獻的人-我正在學習很多可以做和不能做的事情。

import java.util.Random;
public class PalindromeArray
{
static void arrayFill()
{
  int evens = 0, odds = 0;
  int []palindrome = new int [25];
  int j = 0;
  for(int i = 10001; i < 100000; i++) //Start our counter at the first 5-digit palindrome
  {
    String s1 = Integer.toString(i);
    StringBuilder sb = new StringBuilder(s1); //Strings enables us to use sb.reverse for a quick and easy check
    if(s1.equals(sb.reverse().toString()))
    {
        Random r = new Random(); //PRNG so the same numbers don't come up every time
        i = r.nextInt((99999 - 10001) + 1) + 10001; //Ensure generated seed is in desired range
        palindrome[j++] = i;
        if(j == 25)
        {
            break;
        }
    }
  }

  for(int i = 0; i < 25; i++)
  {
    if(palindrome[i]%2 == 0)
    {
        System.out.println("The palindrome at subscript " + i + " = " + palindrome[i] + " --> even");
        evens++;
    }
    else
    {
        System.out.println("The palindrome at subscript " + i + " = " + palindrome[i] + " --> odd");
        odds++;
    }
  }

  System.out.println("\nThe total number of even palindromes is " + evens);
  System.out.println("\nThe total number of odd palindromes is " + odds);
}

public static void main(String[] args)
{
   arrayFill();
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM