Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
忽略isAlindrome()方法-Java中的字母_Java_Oop_Palindrome - Fatal编程技术网

忽略isAlindrome()方法-Java中的字母

忽略isAlindrome()方法-Java中的字母,java,oop,palindrome,Java,Oop,Palindrome,我想知道在检查字符串是否为回文时,如何修改以下方法以忽略某个字母并将其作为通配符 示例:“wows”,在本例中,该方法应返回false,但 “pat”,t可以是通配符(被视为p),因此它返回true “job”,同样,b可以是通配符并被视为j,因此方法返回true 这就是我到目前为止所拥有的,我有一个单独的方法忽略特殊字符和空格,所以在这篇文章中不需要考虑 private static boolean checkPalindrome2(String word) { if(word.len

我想知道在检查字符串是否为回文时,如何修改以下方法以忽略某个字母并将其作为通配符

示例:“wows”,在本例中,该方法应返回false,但

“pat”,t可以是通配符(被视为p),因此它返回true

“job”,同样,b可以是通配符并被视为j,因此方法返回true

这就是我到目前为止所拥有的,我有一个单独的方法忽略特殊字符和空格,所以在这篇文章中不需要考虑

private static boolean checkPalindrome2(String word) {
    if(word.length() < 2) { 
        return true;  
    }
    char first  = word.charAt(0);
    char last   = word.charAt(word.length()-1);
    if(first != last) { 
        return false; 
    }
    else { 
        return checkPalindrome2(word.substring(1,word.length()-1));
    }
}
isAlindromes2是一个调用上述checkPalindrome2方法的方法,我的测试类(单词cat)中的最后一个案例应该返回true,因为t将是通配符字母(通配符,如上所述,替换为c making cat,cac,这是一个回文)

提前感谢所有帮助/输入!!!!
另外,我特意实现了一个递归方法。

只需在基本情况中添加额外的条件:

// both first and last have to NOT be the special character
// and first has to not equal last for this to return false
if(first != special && last != special && first != last)
    return false; 
else
    return checkPalindrome(word.substring(1,word.length()-1));

@别担心,我看你们已经开了这辆奇案的车了。我错过了它,因为我只复制了你的部分代码。什么是适当的方式来声明特殊?你能解释一下这是怎么回事吗?我不完全理解这些条件如何将单个字符作为通配符:S please and thank:)您可以将其传递给回文函数本身:
private static boolean checkPalindrome(String word,char special)
,或者您可以在类中全局定义它
private char special=''对不起,我的意思是如何初始化它****@choloboy7如果通配符可以在任何地方,你必须查看回文的状态,一旦基本条件失败,然后从那里决定它是否关闭了一个字母。
// both first and last have to NOT be the special character
// and first has to not equal last for this to return false
if(first != special && last != special && first != last)
    return false; 
else
    return checkPalindrome(word.substring(1,word.length()-1));