日期:2014-05-20  浏览次数:20705 次

JAVA 随机数取循环如何使其不出现空行
此为主函数


public class AppClass {
public static void main(String[] args)
{ TestClass test = new TestClass();
for(int i = 0 ;i<10;i++){
String str = test.get(10);
System.out.println(str);
}
}
}



此为调用函数
生成随机数的函数

public class TestClass {
public String get(int len)
{

String str = "";
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Random r = new Random(System.currentTimeMillis());
Random r2 = new Random(System.currentTimeMillis());
int len2 = r.nextInt(len);
for(int i=0;i<len2;i++)
{
int a = r.nextInt(10);
String s = Integer.toString(a);
str += s;
}
return str;
}
}



8500
96592
9
27
758
02681
97

857488
11104641


这是十个显示的随机数
可是 会有一个随机数是NULL
即 上面第八行
能不能解决 循环出每一行都是有数字的

random java J2SE

------解决方案--------------------
在return前加个空判断,如果空递归
if (str == null 
------解决方案--------------------
 str.equals("") {
    str = get(len);
}