您现在的位置:龙卷风首页 ›› 网络编程 ›› 阅读文章

chapter 4 practice 5

create an array of string and print them

//: c4/P5.java
// create an array of string and
// print them:
import java.util.*;

public class P5{
 public static void main(String[] args){
 String[] strArray = new String[P5s.pRand(16)];
 System.out.println("Then are " + strArray.length + " elements:");
 System.out.println("-------------------");
 for(int i = 0;i < strArray.length; i++){
  P5s s = new P5s();
  strArray[i] = s.createString();
  System.out.println(strArray[i]);
 }
 System.out.println("-------------------");
 System.out.println("Done.");
}
}
//: create a random string
class P5s{
 static Random rand = new Random();
 static int pRand(int mod){
  return Math.abs(rand.nextInt()) % mod + 1;
 }
 public String createString(){
  char[] c = new char[pRand(26)];
  for(int i = 0;i < c.length;i++){
   c[i] = (char)(61 + pRand(25));
  }
  String s = "";
  for(int i = 0;i < c.length;i++){
   s += c[i];
  }
  return s;
 }
}

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content