java吧 关注:1,294,950贴子:12,828,785
  • 4回复贴,共1

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

只看楼主收藏回复

RT


1楼2013-04-08 10:06回复
    此为主函数
    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);
    }
    }
    }


    2楼2013-04-08 10:21
    回复
      2026-01-17 23:03:04
      广告
      不感兴趣
      开通SVIP免广告
      import java.util.Random;
      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;
      }
      }
      此为调用函数
      生成随机数的函数


      3楼2013-04-08 10:22
      回复
        8500
        96592
        9
        27
        758
        02681
        97
        857488
        11104641
        这是十个显示的随机数
        可是 会有一个随机数是NULL
        即 上面第八行
        能不能解决 循环出每一行都是有数字的


        4楼2013-04-08 10:25
        回复
          好吧
          自己给出答案吧
          在return 前面加一个判断
          if (str == null || str.equals("") ){
          str = get(len);
          }


          5楼2013-04-08 13:32
          回复