2016-02-12 76 views
-3

我需要一些幫助來理解我的代碼的「幕後」部分。我有一個技術援助助理部分代碼,所以我不完全確定它的部分功能。瞭解遞歸代碼

這裏是我的問題(我知道你應該單獨發佈多個問題,但在手,總體而言,這是一個很短的問題。這兩個形影相隨)

  1. 使用遞歸的方法,寫出來的字符串如果輸入了「雷達」這個詞,那麼每一步都要處理這個問題。對「稻草藝術」也一樣。因此,就像遞歸方法一步一步分解雷達和稻草藝術都是迴文一樣。那麼如果我將雷達輸入到我的程序中,它會如何看待幕後?

然後,我只是用一個簡短的迭代法做了迴文同樣的事情需要一些幫助:

  • 使用迭代方法寫一些簡單的代碼(使用循環)來解決這個相同的問題。 (問題在於 - 實現一個使用遞歸方法的程序,該程序確定用戶輸入的字符串是否是迴文。如果字符串與前向相同,則爲迴文,如「雷達」或「塔科貓」)
  • 這裏是我的代碼使用遞歸梅索德解決:

    import java.util.Scanner; 
    public class RecursivePalindrome { 
    
    public static void main(String[] args) 
    { 
        // TODO Auto-generated method stub 
        Scanner keyboard = new Scanner(System.in); 
        System.out.println("Enter a word and I will determine if it is a palindrome");//Gets user input 
        String word = keyboard.nextLine(); 
        String combined = word.replaceAll("[\\W]", "");//matches non-word characters, and removes the spaces. 
        combined = combined.toLowerCase();//makes the combined word all lowercase 
        if(checkPalindrome(word))//calls palindrome method and sees if combined word is a palindrome 
        { 
         System.out.println("The word " + word + " is a palindrome"); 
        } 
        else 
        { 
         System.out.println("The word " + word + " is not a palindrome"); 
        } 
    } 
    
    private static boolean checkPalindrome(String word) 
    { 
        if(word.length() <= 1)//if length is less than or equal to 1 then the string is palindrome 
        { 
         return true; 
        } 
        if(word.charAt(0) == word.charAt(word.length()-1))//checks the first and last char of the string 
        { 
         //if true, then does the same thing with substring 
         //returns only when the string is done checking 
         return checkPalindrome(word.substring(1, word.length()-1)); 
        } 
        //if string doesn't pass check, it's not a palindrome 
        return false; 
    } 
    } 
    
    +0

    事實上,你沒有'?'是一個可能的跡象表明你的問題不清楚。你在問什麼?你不明白什麼? – Whymarrh

    +0

    在運行我的程序時,我需要草編藝術和雷達如何成爲迴文的幫助。所以就像我的代碼一步一步分解到雷達文字,使其通過迴文測試。 – USC23

    回答

    0

    你的第一個問題看似複雜,但它的目的是爲您的遞歸方法中的一些有利地位System.out.println()指令。

    預期的學習效果是,你可以看到那裏實際發生了什麼。

    只需在方法開始處放置一個System.out.println(word)。然後在每個if語句後用更多的System.out.println()與符合條件的語句進行操作。

    要檢查它的迭代,你只需要檢查的第一個字母是等於最後一個字母,第二個字母是等於從後面等的第二個字母:

    public static boolean isPalindrom(String word){ 
        for(int i = 0; i < word.length()/2; i++) { 
         if(word.charAt(i) != word.charAt(word.length()-1-i)) { 
          return false; 
         } 
        } 
        return true; 
    } 
    
    +0

    感謝您的快速響應!我很感激。 – USC23

    +0

    由於錯誤而導致月報。檢查案例「塔科貓」。返回false。 – theWalker

    0

    問題2) ,循環方法

    private static boolean checkPalindrome(String word) 
    { 
        int ii = 0; 
        int io = word.length()-1; 
    
        while(ii<io) 
        { 
         if((word.charAt(ii) == ' ') ii++; 
         else if((word.charAt(io) == ' ') io--; 
         else if(Character.toLowerCase(word.charAt(ii)) 
          != Character .toLowerCase(word.charAt(io))) return false; 
         else { ii++; io--; } 
        } 
        return true; 
    } 
    

    遞歸調用:

    boolean result = checkPalindrome(word, 0, word.lenght()-1); 
    

    PROC

    private static boolean checkPalindrome(String word, int ii, int io) 
    { 
        while((ii<io)&&(word.charAt(ii) == ' ')) ii++; // skipp spaces 
        while((io>ii)&&(word.charAt(io) == ' ')) io--; // skipp spaces 
        if(ii>=io) return true; 
        if(Character.toLowerCase(word.charAt(ii)) 
         == Character .toLowerCase(word.charAt(io))) 
          return checkPalindrome(word, ii++, io--); 
        return false; 
    } 
    
    +0

    任何空格的完整方法 – theWalker

    +0

    我添加了遞歸版本 – theWalker