gpt4 book ai didi

java - 编译保持运行(模式、匹配器任务)

转载 作者:行者123 更新时间:2023-11-29 05:29:16 27 4
gpt4 key购买 nike

当我运行该程序时,它一直在运行并且控制台处于 Activity 状态以供输入,因此我必须按下停止键。这是为什么?(taks是在句子中找到镜像词。使用INTELIJ IDEA)。

public class Blinov11 {

static String mirror(String str){
String mirrored = "";
for (int i = str.length() - 1; i >= 0; i--) {
mirrored += str.charAt(i);
}
return mirrored;
}
public static void main(String[] args) {

String text = new String("If the method atad fi takes data a sekat takes primitive data type as an argument, then the nhet String object representing the primitive data type value is return.");
Pattern pattern = Pattern.compile("\\W\\w+\\W",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
String word = "";
while(matcher.find()){
word = mirror(matcher.group());
if(text.contains(word)){
System.out.println(word);
}
matcher.reset();
}
}
}

最佳答案

那是因为你正在调用:

matcher.reset();

在您的while 循环 中。根据 Java doc :

public Matcher reset()

Resets this matcher. Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.

由于 while 循环中的 reset(),matcher 每次都会重置并且 while (matcher.find()) 变成一个无限循环,因为它总是从起始位置开始查找。

关于java - 编译保持运行(模式、匹配器任务),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21685098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com