Java 我的方法有什么问题
编写一个名为isReverse(“word1”、“word2”)的递归方法,该方法接受两个字符串作为参数,如果两个字符串包含 字符序列彼此相同,但顺序相反,忽略大小写,否则返回false。 例如,调用:Java 我的方法有什么问题,java,recursion,Java,Recursion,编写一个名为isReverse(“word1”、“word2”)的递归方法,该方法接受两个字符串作为参数,如果两个字符串包含 字符序列彼此相同,但顺序相反,忽略大小写,否则返回false。 例如,调用: isReverse("Desserts", "Stressed") 将返回真值。[所以,当你感到压力时,吃甜点?] Null、空和一个字母字符串也将返回true(如果两个参数的值相同)。 这是家庭作业,我很难使这段代码正常工作。不管我做什么,它都是真的 public static boole
isReverse("Desserts", "Stressed")
将返回真值。[所以,当你感到压力时,吃甜点?]
Null、空和一个字母字符串也将返回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;
}
首先,这个集合只有在两个词都为null时才会返回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;
}
}
私有静态布尔值isReverse(字符串a、字符串b){
//确保字符串不为null
如果(a==null | | b==null)返回false;
//如果长度不相等,则无法反转字符串。
如果(a.长度()!=b.长度()){
返回false;
}
//将字符串b转换为数组;
char[]bArray=b.toCharArray();
//创建一个数组,将bArray反向写入其中。
char[]copy=新字符[bArray.length];
//反向遍历bArray并写入副本[]
对于(int i=bArray.length;i<0;i--){
复制[bArray.length-i]=bArray[i];
}
//将副本[]转换回字符串。
字符串检查=String.valueOf(副本);
//查看它们反转的字符串是否等于原始字符串。
如果(检查等信号情况(a)){
返回true;
}否则{
返回false;
}
}
你在说什么
if (firstWord.equalsIgnoreCase(secondWord))
{
return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
}
没关系。但若第一个单词不等于第二个单词呢
它失败并返回真值
您需要添加一个
else
return false;
我还要补充一点,您的空检查将不起作用
if(word1!= null && word2 != null)
{
return false;
}
如果只有在word1或word2为null时才会发生,则该选项没有用处。所以它们在这里不能是空的
如果你成功的话,它会起作用的
if(word1 == null && word2 == null)
{
return true;
}
这是练习吗?递归似乎不是这里最好的选择。不管怎样,你只是删减了一个词,为什么?如果希望比较每个递归调用中的每个字符,则必须修剪这两个单词。而且您甚至没有将经过修剪的单词作为参数传递给递归函数 你缺少的基本情况是基本情况。递归何时必须返回?在您的例子中,在递归的每一步都要减小每个字符串的大小,因此您必须有一个基本大小写来检查大小是否为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;
}
}
首先,我使用递归反转了其中一个字符串(我使用了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;
}
String或StringBuffer类具有相反的method@SrinathGanesh-StringBuilder有相反的说法,但我们必须假设他没有被教过Builder课程。我已经读了三年大学,没有人提到过。考虑到这是家庭作业,我想使用内置函数完成95%的作业是不允许的。你的第二个单词只有一个字符。你能解释一下你在这里做了什么吗?只有代码的答案往往不是很有解释性。