0

我无法让我的其余代码具有以下限制

  1. 它仅由以下字符组成:“R”、“L”和“0”到“9”</li>
  2. “R”字符必须恰好出现两次
  3. “L”字符必须只出现一次
  4. “L”字符必须出现在两个“R”字符之间
  5. 每个“R”和“L”字符后必须至少有一个“0”到“9”字符
  6. 连续出现的“0”到“9”字符不得超过两个

    import java.util.Scanner;
    
    public class HW04
    {
    
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    // Started by naming all the variables 
    String combination;
    // 
    char R, L;
    int length;
    boolean big_R, big_L;    
    System.out.print("Please enter a valid turn dial lock combination : ");
    combination = stdIn.nextLine();
    System.out.println("");
    System.out.println("");
    length = combination.length(); 
    
    if (length <= 9 && length >= 6 )
    {
        R = combination.charAt(0);
        if (R == 'R' )
            big_R = true;
        else
            System.out.println(combination + " is not a valid turn dial lock 
       combination");
        if 
      }
     else 
     {
        System.out.println(combination + " is not a valid turn dial lock 
     combination");
     }
     stdIn.close();
     }
    
     }
    
4

1 回答 1

0

鉴于您的要求列表,这应该是由正则表达式验证的。

也许像 R\d\d?L\d\d?R\d\d?

其内容为:R 后跟 1 位数字和可选的第二位数字 L 后跟 1 位数字和可选的第二位数字,最后是第二个 R 后跟 1 位数字和可选的第二位数字

您可以在其他地方找到如何将其应用于您的代码的示例。

于 2017-10-13T03:42:22.507 回答