【问题标题】:Limiting user input to only 'R', 'L', and 0-9将用户输入限制为仅“R”、“L”和 0-9
【发布时间】:2018-03-25 02:50:25
【问题描述】:

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

  1. 它仅包含以下字符:“R”、“L”和“0”到“9”
  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();
     }
    
     }
    

【问题讨论】:

    标签: java if-statement while-loop limiting


    【解决方案1】:

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

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

    内容如下: 一个 R 后跟 1 个数字和一个可选的第二个数字 L 后跟 1 位数字和可选的第二位数字 最后 第二个 R 后跟 1 个数字和一个可选的第二个数字

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2013-05-27
      • 2014-07-06
      • 2021-08-10
      • 2013-12-29
      • 1970-01-01
      • 2010-11-25
      • 2019-04-12
      相关资源
      最近更新 更多