我有这个程序要编写,不知道如何控制键盘输入:
编写一种方法来检查输入的单词是否有效。 有效的词应该:
- 至少有10个字符
- 以字母开头
- 包含大写字母
- 至少包含3位数字
- 包含特殊字符(例如@,$。%...等)
- 包含空格
醇>
答案 0 :(得分:2)
您不需要在每次击键时验证它,等到用户输入了该单词,然后进行验证。假设您正在使用控制台输入:
System.out.print("Enter something > ");
Scanner input = new Scanner(System.in);
String inputString = input.nextLine();
//perform validations on inputString, heres the first one:
//regex could be used instead of multiple if statements
if(inputString.length() < 10) {
System.out.println("Validation failed, word was too short");
}
else if ...
答案 1 :(得分:0)
您可以使用此正则表达式:
String REGEX = "(^[a-zA-Z](?=.*\\d{3,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%])(?=\\s+).{10,})";
String INPUT = "your password";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
System.out.println("matches(): "+matcher.matches());
^[a-zA-Z] # Start with a letter
(?=.*\\d{3,}) # at least three digits must occur
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%]) # a special character must occur at least once
(?=\\s+) # a space must occur at least once
.{10,} # anything, at least ten places though
$ # end-of-string
要使用此功能,请阅读您的密码(来自档案,摇摆元素......)并在validate(String pass)
内调用validate
方法,然后针对正则表达式进行检查。
答案 2 :(得分:-1)
switch (myapp_config.thisDevice == 'desktop') {
case (primaryNavHeight > pageWrapperHeight):
// do stuff
$('#page-wrapper').css("min-height", primaryNavHeight + "px");
break;
case (primaryNavHeight < pageWrapperHeight):
// do stuff
$('#page-wrapper').css("min-height", primaryNavHeight + "px");
break;
}
//在这里&#34;输入&#34;是一个扫描仪名称,您可以使用任何名称作为OBAMA。
Scanner input=new Scanner(System.in);
System.out.print("Enter Number");
//在这里你可以为&#34; x&#34;。
指定键盘值int x=input.nextInt();
然后你可以打印出来。