学java快一年了,以此来记录下自己的学习足迹
使用软件: IDEA
package StringDemo; import java.util.*; //随即单词生成器 public class RandomWordGenerator { private static Random rand = new Random(); //定义随机类 private static String lowAlpha = "abcdefghijklmnopqrstuvwxyz"; //大写字母 private static String upAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //小写字母 private static String vowel = "aiueo"; //元音 private static String ALL = lowAlpha + upAlpha + vowel;//组合 private static final int DEFAULT_WORD_LENGTH = 10; //默认单词长度 //两个静态方法 public static String getRandomWord() { String str = ""; for(int i = 0; i < DEFAULT_WORD_LENGTH; ++i) { str += ALL.charAt(rand.nextInt(ALL.length())); } return str; } public static String getRandomWord(int len) { //指定单词长度 String str = ""; for(int i = 0; i < len; ++i) { str += ALL.charAt(rand.nextInt(ALL.length())); } return str; } }
以下是测试类
package StringDemo; import java.util.ArrayList; public class AnotherTest { public static void main(String[] args) { ArrayListrandomWordList = new ArrayList (); //随即添加10个指定长度为15的单词 for(int i=0;i<10;++i) { String word = RandomWordGenerator.getRandomWord(15); randomWordList.add(word); } //增强for遍历 for(String word: randomWordList) { System.out.println(word); } } }
最后是测试结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)