求助!用户名由a-z,A-Z,0-9组成,可以是5~10位数,数字不能开头,编程打印出所有用户名,java/c++均可。
答案:3 悬赏:80
解决时间 2021-01-23 10:14
- 提问者网友:藍了天白赴美
- 2021-01-23 07:12
求助!用户名由a-z,A-Z,0-9组成,可以是5~10位数,数字不能开头,编程打印出所有用户名,java/c++均可。
最佳答案
- 二级知识专家网友:举杯邀酒敬孤独
- 2021-01-23 08:34
public static void main(String[] args) {
String allChars="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
long min=62*62*62*62;
long max=min*62*62*62*62*62*62;
for(long j=min;j String s="";
long t=j;
while(t!=0){
s=allChars.charAt((int)(t%62))+s;
t=t/62;
}
if("0123456789".contains(s.charAt(0)+"")) j=j*10-1;//去除数字开头的
else System.out.println(s);
}
}
追问:感觉跟62进制似的,不明觉厉。谢谢大神,我得多想想
String allChars="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
long min=62*62*62*62;
long max=min*62*62*62*62*62*62;
for(long j=min;j
long t=j;
while(t!=0){
s=allChars.charAt((int)(t%62))+s;
t=t/62;
}
if("0123456789".contains(s.charAt(0)+"")) j=j*10-1;//去除数字开头的
else System.out.println(s);
}
}
追问:感觉跟62进制似的,不明觉厉。谢谢大神,我得多想想
全部回答
- 1楼网友:你可爱的野爹
- 2021-01-23 10:09
全部输出也有52*62^9,有点大啊,你确定要?
追问:是啊,关键是函数怎么写
追问:是啊,关键是函数怎么写
- 2楼网友:渊鱼
- 2021-01-23 09:51
不是很清楚你的需求,是要做校验,还是输入再输出
追问:不用那些,就是输出所有满足要求的用户名
追答:
public static void checkUserName(List<String> nameList) {
if (nameList != null && nameList.size() > 0) {
String name = null;
int lowerCaseA = 'a';
int uperCaseA = 'A';
int lowerCaseZ = 'z';
int uperCaseZ = 'Z';
int zeroCode = '0';
int nineCode = '9';
boolean isPrint;
for (int i = 0; i < nameList.size(); i++) {
isPrint = true;
name = nameList.get(i);
char c;
for (int j = 0; j < name.length(); j++) {
c = name.charAt(j);// 依次获取到每个字符做判断
if (j == 0) {
// 首字符判断
if ((c >= lowerCaseA && c <= lowerCaseZ) || (c >= uperCaseA && c <= uperCaseZ)) {
//本if表示首字符是字母开头
}else{
System.err.println(name+" 首字符为非字母开头,不合法");
isPrint = false;
break;
}
}else{
//判断其他字符
if ((c >= lowerCaseA && c <= lowerCaseZ) || (c >= uperCaseA && c <= uperCaseZ) || (c >= zeroCode && c <= nineCode)) {
//本if表示该字符是字母或数字
}else{
System.err.println(name+" 首字符为非字母开头,不合法");
isPrint = false;
break;
}
}
}
if(isPrint){
System.out.println("符合规则的用户名称:"+name);
}
}
}
}
追问:看来你是理解为校验用户输入的nameList是否合法了,这样的话我肯定用正则表达式了,简单太多。
其实我还没有nameList,我是要通过程序输出或者说是得到所有合法的用户名,这个nameList必须已经是全部合法的,所有合法的用户名必须全部在这个nameList里。
不过还是谢谢你,学习了这个方法。
追问:不用那些,就是输出所有满足要求的用户名
追答:
public static void checkUserName(List<String> nameList) {
if (nameList != null && nameList.size() > 0) {
String name = null;
int lowerCaseA = 'a';
int uperCaseA = 'A';
int lowerCaseZ = 'z';
int uperCaseZ = 'Z';
int zeroCode = '0';
int nineCode = '9';
boolean isPrint;
for (int i = 0; i < nameList.size(); i++) {
isPrint = true;
name = nameList.get(i);
char c;
for (int j = 0; j < name.length(); j++) {
c = name.charAt(j);// 依次获取到每个字符做判断
if (j == 0) {
// 首字符判断
if ((c >= lowerCaseA && c <= lowerCaseZ) || (c >= uperCaseA && c <= uperCaseZ)) {
//本if表示首字符是字母开头
}else{
System.err.println(name+" 首字符为非字母开头,不合法");
isPrint = false;
break;
}
}else{
//判断其他字符
if ((c >= lowerCaseA && c <= lowerCaseZ) || (c >= uperCaseA && c <= uperCaseZ) || (c >= zeroCode && c <= nineCode)) {
//本if表示该字符是字母或数字
}else{
System.err.println(name+" 首字符为非字母开头,不合法");
isPrint = false;
break;
}
}
}
if(isPrint){
System.out.println("符合规则的用户名称:"+name);
}
}
}
}
追问:看来你是理解为校验用户输入的nameList是否合法了,这样的话我肯定用正则表达式了,简单太多。
其实我还没有nameList,我是要通过程序输出或者说是得到所有合法的用户名,这个nameList必须已经是全部合法的,所有合法的用户名必须全部在这个nameList里。
不过还是谢谢你,学习了这个方法。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯