package lianxi;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("用户名:");
String enterUserName = sc.nextLine();
boolean flag = checkUserFormat(enterUserName);
System.out.println(flag);
}
//校验键盘录入的用户名是否为正确的格式,并测试。
//正确格式要求(用户名长度必须为6-16位数字或字母)
public static boolean checkUserFormat(String enterUserName) {
boolean flag = true;
//用户名长度必须为6-16位数字或字母
if (enterUserName.length() >= 6 && enterUserName.length() <= 16) {
for (int i = 0; i < enterUserName.length(); i++) {
char ch = enterUserName.charAt(i);
if ( !((ch>='0' && ch <= '9') || (ch>='a' && ch <= 'z') || (ch>='A' && ch <= 'Z')) ) {
flag = false;
break;
}
}
} else {
flag = false;
}
return flag;
}
} |
|