昨天做项目的时候遇到这么个问题,要随机产生6-20位的字符串。最后搞出这么一段代码,不知道好不好。希望各位大哥指导指导。有什么更简单的方法,就给小弟教教哈!谢过先!
public class RandomStr { public static void main(String[] args) {
// 建立字符串数组
String strRandom[] = { "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", "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", "0", "1", "2", "3", "4",
"5", "6", "7", "8", "9" };
// 初始化输出字符串,并置空
String finalStr = "";
// 随机产生1-20之间的数
double randomNum = 20 * Math.random();
int lengthStr = (int) randomNum;
// 如果小于6则给加6,保证产生的随机数在6-20之间
if (lengthStr < 6) {
lengthStr += 6;
}
// 随机字符串长度
for (int count = 0; count <= lengthStr; count++) {
randomNum = 62 * Math.random();
int choiceChar = (int) randomNum;
// 随机字符串数组中字符的位置
// 追加入输出字符串
finalStr += strRandom[choiceChar];
}
System.out.print(finalStr);
}
}
public class RandomStr { public static void main(String[] args) {
// 建立字符串数组
String strRandom[] = { "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", "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", "0", "1", "2", "3", "4",
"5", "6", "7", "8", "9" };
// 初始化输出字符串,并置空
String finalStr = "";
// 随机产生1-20之间的数
double randomNum = 20 * Math.random();
int lengthStr = (int) randomNum;
// 如果小于6则给加6,保证产生的随机数在6-20之间
if (lengthStr < 6) {
lengthStr += 6;
}
// 随机字符串长度
for (int count = 0; count <= lengthStr; count++) {
randomNum = 62 * Math.random();
int choiceChar = (int) randomNum;
// 随机字符串数组中字符的位置
// 追加入输出字符串
finalStr += strRandom[choiceChar];
}
System.out.print(finalStr);
}
}
解决方案 »
- spring+ibatis实现一个通用文件接口遇到的问题
- 关于生成java源码的问题
- Hibernate插入操作时报value larger than specified precision allows for this column错误
- tomact如何配置
- java写个一个程序出现运行问题!望高手赐教!
- 誰幫我看下這個方法什麼意思!
- Servlet问题(高手指点)---- The requested resource (/Servlet1) is not available.
- tapestry tree
- 求教,用IDEA的人遇到过类文件上有个小叉吗?代表什么意思?
- jboss的安装
- SSH实现联级
- JPA Hibernate整合错误~
public static void main(String[] args) {
for(int i=0;i<10;i++){
int num=6+(int)(Math.random()*15);
System.out.println(getString(num));
}
}
private static String getString(int length){
char[] chars=new char[length];
for(int i=0;i<length;i++){
chars[i]=(char)('a'+(int)(Math.random()*26));
}
return new String(chars);
}
}
public class Test {
private static Random random = new Random();
private static String getString(int length){
char[] chars=new char[length];
for(int i=0;i<length;i++){
if(random.nextBoolean()){
chars[i]=(char)('a'+(int)(Math.random()*26));
}else{
chars[i]=(char)('A'+(int)(Math.random()*26));
}
}
return new String(chars);
}
public static void main(String[] args) {
for(int i=0;i<10;i++){
int num=6+(int)(Math.random()*15);
System.out.println(getString(num));
}
}}
"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "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", "0", "1", "2", "3", "4",
"5", "6", "7", "8", "9" }; 3楼和6楼的 都没写出数字的。。
建议楼主就用3楼的加 strRandom数组 就完美了。
char[] chars=new char[length];
for(int i=0;i<length;i++){
if(i==0){
chars[i]=(char)('a'+(int)(Math.random()*26));
}else{
chars[i]=(char)('A'+(int)(Math.random()*26));
}
}
return new String(chars);
}
public static void main(String[] args) {
for(int i=0;i<10;i++){
int num=6+(int)(Math.random()*5);
System.out.println(getString(num));
}
}
}