比较包含一组数字的两个字符串,而不考虑它们在 Java 中的顺序

Comparing two strings containing a set of numbers regardless of their order in Java

我需要比较Java中的两组数字。为了测试程序,两组将是相同的 (123),除了一组将具有不同顺序的“123”。我目前输入“123”作为中奖号码,“321”作为投注号码,使用 .contains() 方法将两者作为字符串进行比较。

我的代码如下:

    if (playNum.contains(winNum))
        System.out.println("True") ;
    else
        System.out.println("False") ;

我正在尝试让程序将“123”的任何变体识别为获胜者。任何帮助将不胜感激。

请看看我的解决方案,如果这是您想要的,请告诉我:

    String input = "123";
    String winner = "321";
    char[] inputArr = input.toCharArray();
    Arrays.sort(inputArr);

    char[] winnerArr = winner.toCharArray();
    Arrays.sort(winnerArr);

    boolean retval = Arrays.equals(inputArr, winnerArr);
    System.out.println(retval);

Here is a little hint code snippet to help you figure out your problem.

Pattern pattern = Pattern.compile(".*[^1-3].*");

   String [] inputs = {"123", "-123" , "123.12", "abcd123"};

   for(String input: inputs){
       System.out.println( "does " + input + " is number : "
                            + !pattern.matcher(input).matches());
   }

   // Regular expression in java to check if String is 6 digit number or not
   String [] numbers = {"123", "123" };
   Pattern digitPattern = Pattern.compile("\d{3}");