"If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0."

"If the number is contained in the text and is repeated, return the sum of the number for each time it is repeated. Otherwise return 0."

我想这样做,以便代码只添加我试图读取的数字的完全唯一版本(即 111 中的 1 给出 3,但 333 中的 33 只给出 33。),但是当我尝试输出结果,像后一个例子这样的实例每个间隔计数一次并将每个计数的实例相加(例如,333 中的 33 给出 66 而不是 33)。

到目前为止,这是我的代码:

public static int sumRepeat(int num, String text)
         {
             int sum = 0;
             String calc = Integer.toString(num);
             int value = text.indexOf(calc);
             for (int i = 0; i < text.length() - calc.length() + 1; i++) {
                 if (text.substring(i, i + calc.length()).equals(calc))
                    sum += num;
             }
             return sum;
         }

有人能帮我把它弄到我没有 运行 进入这个错误和代码 运行s 的地方吗?谢谢。

编辑:抱歉,我的测试用例比我最初所说的要多。这是我所有的测试用例:

sumRepeat(1, "I love CSCE111") returns 3
sumRepeat(12, "The bill is .97") returns 0
sumRepeat(33, "333 bananas for 33 monkeys.") returns 66
sumRepeat(333, "My number is (333)-333-3333") returns 999
sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!") returns 0
sumRepeat(41, "41414141") returns 164
sumRepeat(0, "") returns 0

您应该按输入数字的长度递增,而不是递增 1。 1 只有 1 位长,所以将你的子串移动 1 位有效,而 33 是​​ 2 位长,所以将你的子串移动 1 位会看到 33 两次。

[33]3 和 3[33]

答案是在找到匹配项时增加位数,并且由于您已经处于递增 1 的 for 循环中,所以我们从输入数字的长度中减去 1。

if (text.substring(i, i + calc.length()).equals(calc))
{
    sum += num;
    i += calc.length() - 1;
}

更新(基于更新的问题):

可以使用Java Regex API to solve it. Use the regex, "(?<!\$)(" + String.valueOf(num) + ")" and add each match to sum. Note that ?<! is used for negative lookbehind。在这里,这意味着 num 前面不应有 $

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        System.out.println(sumRepeat(1, "111"));
        System.out.println(sumRepeat(33, "333"));
        System.out.println(sumRepeat(1, "I love CSCE111"));
        System.out.println(sumRepeat(12, "The bill is .97"));
        System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
        System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
        System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
        System.out.println(sumRepeat(41, "41414141"));
        System.out.println(sumRepeat(0, ""));
    }

    public static int sumRepeat(int num, String text) {
        int sum = 0;
        Matcher matcher = Pattern.compile("(?<!\$)(" + String.valueOf(num) + ")").matcher(text);
        while (matcher.find()) {
            sum += num;
        }
        return sum;
    }
}

输出:

3
33
3
0
66
999
0
164
0

非正则表达式解决方案:

您可以使用 String#indexOf(String, int) 从给定索引开始查找 calc 的索引。每次找到这个索引,就把num加到sum上,把这个索引移动num的长度;否则,继续将此索引移动 1.

演示:

public class Main {
    public static void main(String[] args) {
        System.out.println(sumRepeat(1, "111"));
        System.out.println(sumRepeat(33, "333"));
        System.out.println(sumRepeat(1, "I love CSCE111"));
        System.out.println(sumRepeat(12, "The bill is .97"));
        System.out.println(sumRepeat(33, "333 bananas for 33 monkeys."));
        System.out.println(sumRepeat(333, "My number is (333)-333-3333"));
        System.out.println(sumRepeat(87, "I can't believe Aragorn is 83 years old. 83!"));
        System.out.println(sumRepeat(41, "41414141"));
        System.out.println(sumRepeat(0, ""));
    }

    public static int sumRepeat(int num, String text) {
        int sum = 0;
        String calc = Integer.toString(num);
        int len = calc.length();
        int i = 0;
        while (i < text.length()) {
            int index = text.indexOf(calc, i);
            if (index != -1) {
                if (index == 0 || text.charAt(index - 1) != '$') {// Check to exclude num preceded by $
                    sum += num;
                    i = index + len;
                } else {
                    i++;
                }
            } else {
                i++;
            }
        }
        return sum;
    }
}

输出:

3
33
3
0
66
999
0
164
0

原解:

修改循环计数器以i < text.length()终止并以num的步长值继续。

public class Main {
    public static void main(String[] args) {
        System.out.println(sumRepeat(1, "111"));
        System.out.println(sumRepeat(33, "333"));
    }

    public static int sumRepeat(int num, String text) {
        int sum = 0;
        String calc = Integer.toString(num);
        for (int i = 0; i < text.length(); i += num) {
            if (text.substring(i, i + calc.length()).equals(calc)) {
                sum += num;
            }
        }
        return sum;
    }
}

输出:

3
33