2013-12-11 94 views
2
  • 列表項

我在Java中創建一個迴文程序,它通過用戶輸入的字或詞,並將它們放入一個數組。然後它接受反向輸入並將其放入另一個數組中。然後我檢查兩個字是否相同。迴文程序沒有給予正確的輸出在Java中

我的問題是,程序正在改變其中一個字母,以便它總是正確的,並且它迭代了太多次。任何想法,在我的代碼是什麼造成這個?

public static boolean isPalindrome(char[] a, int used){ 
char[] original = a; 
int newNumber = used - 1; 
for(int i = 0; i <= newNumber; i++){ 
    a[i] = a[newNumber - i ]; 
    System.out.println("Your original word was: " + String.valueOf(original)); 
    System.out.println("Backwards, your word is: " + String.valueOf(a)); 

    } 
if(String.valueOf(original).equalsIgnoreCase(String.valueOf(a))){ 
    System.out.println("Your word or words are palindromes."); 

}else{ 
    System.out.println("Your word or words are not palindromes"); 
} 
return(String.valueOf(original).equalsIgnoreCase(String.valueOf(a))); 
} 

public void userInput(){ 
Scanner keyboard = new Scanner(System.in); 
System.out.println("Enter a word or words that you believe are palindromes: "); 
word = keyboard.next(); 
word = word.replaceAll("\\W",""); 
int length = word.length(); 
char[] p = new char[length]; 
for(int i = 0; i < length; i++){ 
    p[i] = word.charAt(i); 
} 
Palindrome.isPalindrome(p, length); 

}

這是我的輸出,當我輸入 「糖果」。

Your original word was: yandy 
Backwards, your word is: yandy 
Your original word was: ydndy 
Backwards, your word is: ydndy 
Your original word was: ydndy 
Backwards, your word is: ydndy 
Your original word was: ydndy 
Backwards, your word is: ydndy 
Your original word was: ydndy 
Backwards, your word is: ydndy 
Your word or words are palindromes 

感謝大家的回答! 下面是正確的代碼這樣的作品,雖然效率不高,因爲它可以:

public static boolean isPalindrome(char[] a, int used){ 
    char[] original = a; 
    char[] newArray = new char[used]; 
    int newNumber = used - 1; 
    for(int i = 0; i <= newNumber; i++){ 
     newArray[i] = a[newNumber - i ]; 
     } 
    System.out.println("Your original word was: " + String.valueOf(original)); 
    System.out.println("Backwards, your word is: " + String.valueOf(newArray)); 

    if(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray))){ 
     System.out.println("Your word or words are palindromes."); 

    }else{ 
     System.out.println("Your word or words are not palindromes"); 
    } 
    return(String.valueOf(original).equalsIgnoreCase(String.valueOf(newArray))); 
    } 
+1

那麼什麼是你的輸出? – SimplyPanda

回答

0

問題是與您的代碼a[i] = a[newNumber - i ];要更改現有陣列。

首先創建新的數組,然後把你的反向字符串。

public static boolean isPalindrome(char[] a, int used) { 
    char[] newA = new char[a.length]; 
    int newNumber = used - 1; 
    for (int i = 0; i <= newNumber; i++) { 
     newA[i] = a[newNumber - i]; 
     System.out.println("Your original word was: " + String.valueOf(a)); 
     System.out.println("Backwards, your word is: " + String.valueOf(newA)); 
    } 
    if (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a))) { 
     System.out.println("Your word or words are palindromes."); 

    } else { 
     System.out.println("Your word or words are not palindromes"); 
    } 
    return (String.valueOf(String.valueOf(newA)).equalsIgnoreCase(String.valueOf(a))); 
} 

還有很多其他有效的方法來編寫此代碼。

+0

嗯,我是故意這樣做的。我將原始數組存儲在新數組「original」中,然後更改了[i] = a [newNumber - i]; – TEKRuby

+0

在這種情況下,通過char [] original = new char [a.length];來創建新數組「original」。並通過迭代數組a來在其中放置元素。我已經相應地更新了你的方法 – Mahendra

+0

你是對的創建一個新的數組。我改變了它,現在它工作。我還必須放置System.out.println(); for循環之外。謝謝! – TEKRuby

0

如果你想在這個簡單的方法來檢查palindrome,你可以嘗試StringBuilder

public static boolean isPalindrome(String input) { 
    StringBuilder sb=new StringBuilder(input); 
    return input.equals(sb.reverse().toString()); 
} 

在你的情況改變你isPalindrome方法如下

public static void isPalindrome(char[] a, int used){ 
    char[] newArr = new char[a.length]; // create a new array in same length as a 
    int newNumber = used - 1; 
    System.out.println("Your original word was: " + String.valueOf(a)); 
    for(int i = 0; i <= newNumber; i++){ 
     newArr[i] = a[newNumber - i ]; 
    } 
    System.out.println("Backwards, your word is: " + String.valueOf(newArr)); 
    if(String.valueOf(newArr).equalsIgnoreCase(String.valueOf(a))){ 
     System.out.println("Your word or words are palindromes."); 
    }else{ 
     System.out.println("Your word or words are not palindromes"); 
    } 
} 
+0

謝謝,但這是一個任務,所以我不得不做一個特定的方式。 – TEKRuby

+0

@TEKRuby好的,建議你的代碼也改變 –

2

在您的迴文功能: -

for(int i = 0; i <= newNumber; i++){ 
      a[i] = a[newNumber - i ]; 
      System.out.println("Your original word was: " + String.valueOf(original)); 
      System.out.println("Backwards, your word is: " + String.valueOf(a)); 

      } 

這裏要替換現有的陣列。

你甚至可以嘗試下面的代碼: -

String original, reverse=""; 
       Scanner in = new Scanner(System.in); 

       System.out.println("Enter a string to check if it is a palindrome"); 
       original = in.nextLine(); 

       int length = original.length(); 

       for (int i = length - 1 ; i >= 0 ; i--) 
       reverse = reverse + original.charAt(i); 

       if (original.equals(reverse)) 
       System.out.println("Entered string is a palindrome."); 
       else 
       System.out.println("Entered string is not a palindrome."); 
+0

感謝您的建議。我最終創建了一個反向詞的新陣列。 – TEKRuby

0
  1. 沒有必要的陣列(只複製referenz)複製,只有創造具有相同的大小和使用的新數組作爲原始數組,使用a

    char[] reverse = new char[used]; 
    
  2. system.out,出來的for循環:

    for(int i = 0; i <= newNumber; i++){ 
        reverse[i] = a[newNumber - i ]; 
    } 
    System.out.println("Your original word was: " + String.valueOf(a)); 
    System.out.println("Backwards, your word is: " + String.valueOf(reverse)); 
    

整個isPalindrom功能:

public static boolean isPalindrome(char[] a, int used){ 
    char[] reverse = new char[used]; 
    int newNumber = used -1; 
    for(int i = 0; i <= newNumber; i++){ 
     reverse[i] = a[newNumber - i ]; 
    } 
    System.out.println("Your original word was: " + String.valueOf(a)); 
    System.out.println("Backwards, your word is: " + String.valueOf(reverse)); 

    if(String.valueOf(reverse).equalsIgnoreCase(String.valueOf(a))){ 
     System.out.println("Your word or words are palindromes."); 

    }else{ 
     System.out.println("Your word or words are not palindromes"); 
    } 
    return(String.valueOf(reverse).equalsIgnoreCase(String.valueOf(a))); 
} 
0

的問題是在你指定的

isPalindrome方法
char[] original = a; 

炭[]變量的引用。通過設置original = a,您正在複製引用,所以現在這兩個數組都指向同一塊內存。

現在,當您修改a時,original也會被修改,因爲它們都指向相同的內存。

您可以通過創建一個新的實例,如下所示解決此問題:

char[] original = new char [a.length]; 
    System.arraycopy(a, 0, original, 0, a.length); 
相關問題