我的方法isReverse有什么问题

时间:2015-04-26 03:29:13

标签: java recursion

编写一个名为isReverse的递归方法(" word1"," word2"),它接受两个字符串作为参数,如果两个字符串包含,则返回true 相同的字符序列但相反的顺序,忽略大小写,否则返回false。 例如,调用:

isReverse("Desserts", "Stressed") 

会返回true。 [当你有压力的时候吃甜点吗?] 空,空和一个字母字符串也将返回true(如果两个参数都是相同的值)。 这是家庭作业,我无法使这段代码正常工作。无论我做什么,它都会返回。

public static boolean isReverse(String word1, String word2) 
{
    if(word1 == null || word2 == null)
    {
        if(word1!= null && word2 != null)
        {
            return false;
        }
        return false;
    }
    else if(word1.length() == word2.length())
    {
        String firstWord = word1.substring(0, word1.length());
        String secondWord = word2.substring(word2.length()-1);
        if (firstWord.equalsIgnoreCase(secondWord))
        { 
            return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
        }
    }
    return true;
}

4 个答案:

答案 0 :(得分:1)

首先,你有这个设置,只有当两个单词都为空时它才会返回false;如果它们不为null,则您重新调用该方法(如果长度相等),则返回true。

private static boolean isReverse(String a, String b) {
    // make sure the strings are not null
    if(a == null || b == null) return false;

    // If the lengths are not equal, the strings cannot be reversed.
    if(a.length() != b.length()) {
        return false;
    }

    // Convert string b to an array;
    char[] bArray = b.toCharArray();

    // Create an array to write bArray into in reverse.
    char[] copy = new char[bArray.length];

    // Iterate through bArray in reverse and write to copy[]
    for(int i = bArray.length; i < 0; i--) {
        copy[bArray.length - i] = bArray[i];
    }

    // Convert copy[] back into a string.
    String check = String.valueOf(copy);

    // See if they reversed string is equal to the original string.
    if(check.equalsIgnoreCase(a)) {
        return true;
    } else {
        return false;
    }
}

答案 1 :(得分:1)

你在说

    if (firstWord.equalsIgnoreCase(secondWord))
    { 
        return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
    }

没关系。但是如果firstWord不等于第二个词

那该怎么办?

它会通过并返回true。

您需要添加

else
    return false;

我还要补充一点,你的空检查不起作用。

    if(word1!= null && word2 != null)
    {
        return false;
    }

没有用,因为只有在word1或word2为null时才会出现这种情况。所以它们在这里不能为null和null。

如果你做到了它会起作用

    if(word1 == null && word2 == null)
    {
        return true;
    }

答案 2 :(得分:0)

这是一个练习吗?递归似乎不是这里的最佳选择。无论如何,你只是修剪一个词,为什么?如果您希望比较每个递归调用中的每个字符,则必须修剪两个单词。而你甚至没有将修剪过的单词作为参数传递给递归函数!

你遗漏的基本事情是一个基本案例。递归时必须返回?在您的情况下,您在每个递归步骤中减少每个字符串大小,因此您必须有一个基本案例来检查大小是否为1。

希望这段代码清楚你的想法:

public static boolean isReverse(String word1, String word2) {
    if (word1 == null || word2 == null) {
        return false;
    }
    if (word1.length() == 1 && word2.length() == 1) {
        //Used equals just for fast compare
        return word1.equals(word2);
    } else if (word1.length() == word2.length()) {
        if (word1.charAt(0) == word2.charAt(word2.length() - 1)) {
            String firstWord = word1.substring(1, word1.length());
            String secondWord = word2.substring(0, word2.length() - 1);
            System.out.printf("Trimmed %s, %s to %s, %s\n", word1, word2, firstWord, secondWord);
            return isReverse(firstWord, secondWord);
        } else {
            //Characters didn't matched
            return false;
        }
    } else {
        //Lenght doesn't match
        return false;
    }
}

答案 3 :(得分:0)

首先,我使用recursion.then来反转其中一个字符串(我使用了word1),然后将第二个字符串与两个字符串相等,结果设置为true。

public static boolean isReverse(String word1, String word2)
{
    boolean result  = false;
    //check null to avoid null pointer exception
    if(word1 == null | word2 == null){
        result = false;
    }else if(word1.length() == word2.length()){

         word1 = reverseString(word1);
         if(word1.equalsIgnoreCase(word2)){
             result = true;
         }
    }
    return result;

}   

static String reverse = "";
public static String reverseString(String str){

    if(str.length() == 1){
        reverse+=str;

    } else {
        reverse += str.charAt(str.length()-1)
                +reverseString(str.substring(0,str.length()-1));
    }
    return reverse;
}