完整代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;
public class Number {
/*
* 编写自动脚本 在1-500的范围内生成随机整数 且不能为17的倍数 共生成1000000个数字 以英文逗号分隔,并去除以2开头以7结尾的数字
*/
public void printNunmer() {
Listlist = new ArrayList ();
// 以2开头
String pattern0 = "^2[0-9]{0,2}$";
// 以7结尾
String pattern1 = "^[0-9]{0,2}7$";
// 定义一个变量作为判断条件
long i = 1;
// 在循环里让i自增 当i>1000000 跳出循环
while (i <= 1000000) {
// 随机生成一个整数
Integer content = new Random().nextInt(500);
// 当该整数不是17的倍数并且不以2开头和或不以7结尾时 把该整数放入list中 并且让i自增
if (!(content % 17 == 0 || Pattern.matches(pattern0, content + "")
|| Pattern.matches(pattern1, content + ""))) {
list.add(content);
i++;
}
}
for (Integer i1 : list) {
System.out.print(i1 + ",");
}
System.out.println();
System.out.println("一共有" + list.size() + "个数字");
}
public static void main(String[] args) {
Number number = new Number();
number.printNunmer();
}
}
测试结果:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;
public class Randomer {
/**
* 最大产生随机数数量
*/
private final static int MAX_RANDOM_SIZE = 1000000;
/**
* 产生随机数范围
*/
private final static int RANDOM_SEED = 500;
/**
* 分隔符
*/
private final static String SPLIT = ",";
/**
* 结果输出路径
*/
private final static String DATA_OUTPUT_PATH = "/Users/liuchongguang/Downloads/random_result.txt";
/**
* 换行符
*/
private final static String CRLF = "\r\n";
/**
* 每行输出随机数个数
*/
private final static int RANDOM_PER_LINE = 20;
public static void main(String[] args) throws Exception {
Random random = new Random();
File file = new File(DATA_OUTPUT_PATH);
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
int count = 0;
while (count < MAX_RANDOM_SIZE) {
int num = random.nextInt(RANDOM_SEED) + 1;
String str = String.valueOf(num);
if ((num % 17 != 0) && !str.startsWith("2") && !str.endsWith("7")) {
count++;
bw.write(str);
if (count < MAX_RANDOM_SIZE) {
bw.write(SPLIT);
if (count % RANDOM_PER_LINE == 0) {
bw.write(CRLF);
}
}
}
}
bw.close();
fw.close();
}
}
package eg;
import java.util.Random;
public class RandomNumber {
public static void main(String[] args) {
RandomNumber rn = new RandomNumber();
Rule rule = new Rule() {
@Override
protected boolean isAvailable(Integer number) {
if (number%17 != 0 && !number.toString().startsWith("2") && !number.toString().endsWith("7")) {
return true;
}
return false;
}
};
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 1000000; i++) {
Integer num = rn.getRandomNumber(1, 500, rule);
if (buffer.length() == 0) {
buffer.append(num);
} else {
buffer.append(", " + num);
}
}
System.out.println(buffer.toString());
}
public Integer getRandomNumber(Integer min, Integer max, Rule rule) {
Random rdm = new Random();
Integer number = rdm.nextInt(max) + min;
if (rule.isAvailable(number)) {
return number;
} else {
return getRandomNumber(min, max, rule);
}
}
}
abstract class Rule {
protected abstract boolean isAvailable(Integer number);
}