问题描述
java1。产生一个随机验证码,其格式是一个数字和一个字母一个数字一个字母例如2q3r2.给定以下字符串:李四:67|张三:88|王五:76要求拆分结果如下:李四-->67张三-->88王五-->76
解决方案
解决方案二:
产生随机数可以用Random的nextInt(intn)。产生随机字母:你可以先定义26个字母组成的字符数组。然后用Random的nextInt(25)产生字母下标,这样就变相产生了随机字母。字符串拆分参考String的split(Stringregex)方法
解决方案三:
产生随机字母应该用Random的nextInt(26)产生字母下标,不好意思。
解决方案四:
publicstaticvoidmain(String[]args){////第一题Randomrandom=newRandom();int[]numbers={0,1,2,3,4,5,6,7,8,9};char[]characters={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};intone=numbers[random.nextInt(numbers.length)];chartwo=characters[random.nextInt(characters.length)];intthree=numbers[random.nextInt(numbers.length)];charfour=characters[random.nextInt(characters.length)];System.out.printf("验证码:%d%s%d%sn",one,two,three,four);//第二题//Stringtext="李四:67|张三:88|王五:76";text=text.replace(":","-->");String[]array=text.split("\|");for(inti=0;i<array.length;i++){System.out.println(array[i]);}}
解决方案五:
引用3楼littlebrain4solving的回复:
publicstaticvoidmain(String[]args){////第一题Randomrandom=newRandom();int[]numbers={0,1,2,3,4,5,6,7,8,9};char[]characters={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};intone=numbers[random.nextInt(numbers.length)];chartwo=characters[random.nextInt(characters.length)];intthree=numbers[random.nextInt(numbers.length)];charfour=characters[random.nextInt(characters.length)];System.out.printf("验证码:%d%s%d%sn",one,two,three,four);//第二题//Stringtext="李四:67|张三:88|王五:76";text=text.replace(":","-->");String[]array=text.split("\|");for(inti=0;i<array.length;i++){System.out.println(array[i]);}}
这个就可以用如果字母大小写都用,就在数组里面加上小写的