这是我到目前为止所做的递归,但它似乎不正确,这主要是出于兴趣。任何帮助或提示将不胜感激。
public class CompareStrings {
public static boolean match(String x, String y) {
//turn each string into a char[], sort that array,
//then compare the two Simple
char[] first = x.toCharArray();
char[] second = y.toCharArray();
java.util.Arrays.sort(first);
java.util.Arrays.sort(second);
String sorted_str1 = new String(x);
String sorted_str2 = new String(y);
if(sorted_str1.equals(sorted_str2)){
return true;
}
else{
return false;
}
}
public static void main(String args[]) {
System.out.println(match("hello", "hello.")); // should return false
System.out.println(match("hello", "jello")); // should return false
System.out.println(match("hello", "h@llo")); // should return true
System.out.println(match("hello", "h@@@@")); // should return true
System.out.println(match("hello", "h*")); // should return true
System.out.println(match("hello", "*l*")); // should return true
System.out.println(match("anyString", "*")); // should return true
}
}
最佳答案
您必须记住,如果您想使用递归,则必须在函数内部使用函数。看看和例子:
void myMethod( int counter)
{
if(counter == 0)
else
{
System.out.println("hello" + counter);
myMethod(--counter);
System.out.println(""+counter);
}
}
我的函数是 myMethod,我在其内部使用它。但你没有这样做,所以你没有使用递归。
关于java - 我怎样才能得到底部结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15721307/