正则表达式被无限循环卡住了(真)

问题描述 投票:0回答:2

我刚开始java.util.regex主题。我可以执行以下代码并获得满意的结果。我在代码中使用了无限循环,试图通过给出条件来打破。然而它并没有以某种方式打破。有什么想法吗?

import java.util.Scanner;
import java.util.regex.*;
import javax.swing.JOptionPane;

public class RegEx {
    public static void main(String[] args){

        //using system input for Pattern and Matcher classes
        Scanner regex = new Scanner(System.in);

        while(true){
            System.out.println("regex please: ");
            Pattern p1 = Pattern.compile(regex.nextLine());
            System.out.println("input please: ");
            Matcher m1 = p1.matcher(regex.nextLine());
            boolean b2 = m1.matches();
            System.out.println("your input \""+m1.group()+"\" is matching and "+b2);

            if(JOptionPane.showInputDialog("does it match: ").equals("Yes")){
                System.out.println("take rest now");
                break;
            }//note sure why this condition is not working
        }
    }
}
java regex while-loop
2个回答
0
投票

尝试使用

if(JOptionPane.showInputDialog("does it match: ").trim().equalsIgnoreCase("yes"))
{
//your code
}

0
投票

只需输入没有空格的Yes,并注意Y在下面的弹出窗口中是大写的,然后按下OK按钮。

enter image description here

执行此操作时,if中的条件将变为true,并且由于take rest now语句,您的代码将在离开循环之前在控制台中输出字符串break

if(JOptionPane.showInputDialog("does it match: ").equals("Yes")){
       System.out.println("take rest now");
       break;
}
© www.soinside.com 2019 - 2024. All rights reserved.