簡體   English   中英

將用戶輸入限制為僅“ R”,“ L”和0-9

[英]Limiting user input to only 'R', 'L', and 0-9

我無法讓我的其余代碼具有以下限制

  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(); } } 

給定您的要求列表,此提示也應通過正則表達式進行驗證。

也許像R\\d\\d?L\\d\\d?R\\d\\d?

讀為:R后跟1位數字和可選的第二位數字,L后跟1位數字和可選的第二位數字,最后是第二個R后跟1位數字和可選的第二位數字。

您可以在其他地方找到有關如何將其應用於代碼的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM