*"与&QUOT Java的递归字符串比较;作为一个通配符递归、通配符、作为一个、字符串

2023-09-11 07:07:27 作者:淡寫伱惪情

我正在写检查字符串中的每个字母来比较它们的递归方法。我在做任何的*字符匹配的麻烦,并在需要时充当尽可能多的字母。 (使它成为一个通配符)

I'm writing a recursive method that checks each letter of the string to compare them. I'm having trouble making the "*" character match with any, and act as as many letters as needed. (Making it a wildcard)

我在想,如果有人可以给我一个提示上,将使用的算法?

I was wondering if someone can give me a hint on the algorithm that would be used?

下面是我到目前为止所。

Here is what I have so far.

public static boolean match(String x, String y) {
    return match_loop(x, y, 0, 1);
}

public static boolean match_loop(String a, String b, int i, int s) {
    try {
        if (a == b) {
            return true;
        }

        if (i >= a.length() && i >= b.length()) {
            return true;
        }

        if (a.charAt(i) == b.charAt(i)) {
            return match_loop(a, b, i + 1, s);
        }


        //(((...A bunch of if statements for my other recursion requirements

        return false;

    } catch (java.lang.StringIndexOutOfBoundsException e) {
        return false;
    }
}

public static void main(String[] args) {
    System.out.println(match("test", "t*t")); // should return true
}

我在想什么做的是增加另一个实际参数的的方法,一个int,将作为一个字母backcounter。基本上,我在想这个 如果a或b的字符(是)(S原本是1)是*,回忆起与S + 1的递归。 然后几个不同的IFS语句来修正错误。然而,这种方法似乎很长的和重复的。是否有任何其他的算法,我可以使用?

What I was thinking of doing is adding another arguement to the method, an int that will act as a letter backcounter. Basically I'm thinking of this if a or b at char(i-s) (s originally being 1.) is a *, recall the recursion with s+1. and then a few more different ifs statements to fix the bugs. However this method seems really long and repetitive. Are there any other algorithms I can use?

推荐答案

不要使用 == 字符串值比较。使用等于()方法。

Do not use == for String value comparison. Use the equals() method.

若(a ==二)如果a.equals(B)