Java 理解递归代码

Java 理解递归代码,java,Java,我需要一些帮助来理解代码的“幕后”部分。我让助教帮我处理部分代码,所以我不完全确定其中的一些功能 以下是我的问题(我知道你应该分别发布多个问题,但这两个问题是同时进行的,总体而言,这是一个非常简短的问题。) 使用递归方法,如果输入了单词“radar”,则写出每个步骤正在处理的字符串。“稻草艺术”也是如此。就像一步一步的分解递归方法如何得出雷达和稻草艺术都是回文。那么,如果我把雷达输入到我的程序中,它在幕后会是什么样子呢 然后我需要一些帮助,使用一个简单的迭代方法,对回文做同样的事情: 使用迭代方

我需要一些帮助来理解代码的“幕后”部分。我让助教帮我处理部分代码,所以我不完全确定其中的一些功能

以下是我的问题(我知道你应该分别发布多个问题,但这两个问题是同时进行的,总体而言,这是一个非常简短的问题。)

  • 使用递归方法,如果输入了单词“radar”,则写出每个步骤正在处理的字符串。“稻草艺术”也是如此。就像一步一步的分解递归方法如何得出雷达和稻草艺术都是回文。那么,如果我把雷达输入到我的程序中,它在幕后会是什么样子呢
  • 然后我需要一些帮助,使用一个简单的迭代方法,对回文做同样的事情:

  • 使用迭代方法(使用循环)编写一些简单代码来解决同样的问题。(问题在于-实现一个程序,该程序使用递归方法确定用户输入的字符串是否为回文。如果字符串向前与向后相同,则为回文,如“雷达”或“塔可猫”。)
  • 下面是我使用递归方法解决的代码:

    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;
    }
    }
    
    import java.util.Scanner;
    公共类递归回文{
    公共静态void main(字符串[]args)
    {
    //TODO自动生成的方法存根
    扫描仪键盘=新扫描仪(System.in);
    System.out.println(“输入一个单词,我将确定它是否是回文”);//获取用户输入
    字符串字=键盘.nextLine();
    String combined=word.replaceAll(“[\\W]”,“”);//匹配非单词字符,并删除空格。
    combined=combined.toLowerCase();//使组合词全部小写
    if(checkPalindrome(word))//调用palindrome方法并查看组合词是否为回文
    {
    System.out.println(“单词“+word+”是回文”);
    }
    其他的
    {
    System.out.println(“单词“+word+”不是回文”);
    }
    }
    私有静态布尔校验回文(字符串字)
    {
    
    if(word.length()您的第一个问题可能看起来很复杂,但它针对的是递归方法中的一些位置良好的
    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;
    }
    
    public静态布尔值isAlindrom(字符串字){
    for(int i=0;i
    
    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;
    }
    
    过程

    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;
    }
    
    private静态布尔校验回文(字符串字,int-ii,int-io)
    {   
    while((iii)和&(word.charAt(io)='')io--;//跳过空格
    如果(ii>=io)返回true;
    if(字符到小写(单词字符(ii))
    ==Character.toLowerCase(word.charAt(io)))
    返回检查回文(word,ii++,io--);
    返回false;
    }
    
    您没有“?”这一事实可能表明您的问题不清楚。您在问什么?您不明白什么?我需要帮助了解在运行我的程序时,straw arts和radar将如何成为回文。因此,就像一步一步地细分我的代码对单词radar的作用,以使其通过回文测试。Tha感谢您的快速响应!我很感激。请更正错误。检查“Taco cat”案例。返回false。使用任意空格添加递归版本的完整方法
    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;
    }