[英]Limiting user input to only 'R', 'L', and 0-9
我無法讓我的其余代碼具有以下限制
連續出現不超過兩個“ 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.