Java &引用;如果该数字包含在文本中且重复出现,则返回每次重复出现的数字之和。否则返回0。”;
我想让代码只添加我试图读取的数字的完全唯一版本(即111中的1给出3,但333中的33只给出33.),但当我尝试输出结果时,后一个示例这样的实例每间隔计数一次,并将每个计数的实例相加(例如,333中的33给出66而不是33) 以下是我目前的代码:Java &引用;如果该数字包含在文本中且重复出现,则返回每次重复出现的数字之和。否则返回0。”;,java,integer,addition,Java,Integer,Addition,我想让代码只添加我试图读取的数字的完全唯一版本(即111中的1给出3,但333中的33只给出33.),但当我尝试输出结果时,后一个示例这样的实例每间隔计数一次,并将每个计数的实例相加(例如,333中的33给出66而不是33) 以下是我目前的代码: public static int sumRepeat(int num, String text) { int sum = 0; String calc = Integer.toSt
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;
}
不应增加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;
}
更新(基于更新的问题):
您可以使用来解决它。使用regex,
”(?虽然您的代码确实帮助缓解了我在问题中遇到的问题,但现在我试图破译的一些其他案例给了我错误。具体来说,这些是我试图解决的测试案例:sumRepeat(1,“我爱CSCE111”)返回3个sumRepeat(12,“账单是12.97美元”)返回0 sumRepeat(33,“33只猴子吃333根香蕉”)返回66 sumRepeat(333,“我的号码是(333)-333-3333”)返回999 sumRepeat(87,“我不敢相信阿拉贡83岁了。83!”)返回0 sumRepeat(41,“414141”)返回164 sumRepeat(0“)返回0如果您对这些案例有任何提示,请告诉我。我不确定我是否理解此问题。您对每个案例的预期结果是什么?您可以用这些新案例修改您的问题吗?好的,我将编辑我的初始响应以添加我的测试案例。很抱歉不知从何处将这些推给您。我可以看到您已编辑了d你的问题。为什么要重复(12,“账单是12.97美元”)
return0
?我不是100%确定原因,但我相信这可能是因为它前面的$或数字是12.97而不是12。在这种情况下,我想说可能会添加一个特殊情况,如果数字后面的$不应该计算?如果您对您的作业规范的含义有疑问,或者我如果你怀疑提供的输入和预期的相应输出没有意义,你应该向你的导师而不是我们寻求澄清。我现在要睡觉了。我明天会回应你的评论。我不允许使用导入来解决这个问题。有没有其他不涉及使用导入的解决方案?@KaitoMeagher-我在更新部分发布了一个非正则表达式解决方案。如果您有任何进一步的疑问/问题,请随时发表评论。
if (text.substring(i, i + calc.length()).equals(calc))
{
sum += num;
i += calc.length() - 1;
}
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 $12.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
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 $12.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
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