Post date: Apr 4, 2012 3:05:17 AM
此代码原目的是为了在随机区组中,产生完全随机区组设计的分布图。
java原代码如下:
import java.util.*;import java.io.*;public class RandomCompleteBlockDesign {    Random random = new Random();    int ranNum = 0;    int countNum = 0;    int block = 0; // 区组    int plotNum = 0; // 小区数目    int[] scopeArray; // 记录所需要产生的plot数    int[] randomArray; // 记录随机产生的plot序列    int choice; // 记录结果的输出方向(1定向 到命令窗口,2为定向到文件)    File output;    public static void main(String[] args) {        RandomCompleteBlockDesign rCBD = new RandomCompleteBlockDesign();        PrintStream console = System.out; //重定向到默认输出,即命令提示符         System.out.print("Plots distribution of Random Complete Block Design \n");
        Scanner scan = new Scanner(System.in);        System.out.println("please, input the blocks number: ");        rCBD.block = scan.nextInt();        System.out.println("please, input the plots number:");        rCBD.plotNum = scan.nextInt();        System.out.print("Print on the Command Window or Record in txt file!\n"            + "1 for the Command Window.\n" + "2 for the text file.\n"            + "input your choice!\n");        rCBD.choice = scan.nextInt();        if (rCBD.choice == 1) {            rCBD.display();        } else if (rCBD.choice == 2) {            System.out            .println("please, input the path of the file! such as c://javatest/randomdesign.txt");            String path = scan.next();            PrintStream ps = null;            rCBD.output = new File(path);            try {                if (!rCBD.output.exists()) {                    rCBD.output.createNewFile();                }                ps = new PrintStream(new FileOutputStream(rCBD.output));                System.setOut(ps);                rCBD.display();            } catch (FileNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } finally {                if (ps != null) {                        ps.close();                }            }        } else {            System.out.println("Restart the program!......");        }        System.setOut(console);        System.out.println();        System.out.println("================finish==============");    }    public void display() {        createdPlot();        for (int i = 0; i < block; i++) {            createRandomPlot();            System.out.println("The " + (i + 1)            + " block's plots distribution!");            for (int j = 1; j <= randomArray.length; j++) {                System.out.print(randomArray[j - 1] + "\t");                if (j % 10 == 0)                    System.out.println();                }                randomArray = null;             }        }    public void createRandomPlot() {        randomArray = new int[plotNum];        outerLoop: while (true) {            ranNum = random.nextInt(plotNum);            for (int i = 0; i < countNum; i++) {                if (scopeArray[ranNum] == randomArray[i])                    continue outerLoop;                }            randomArray[countNum] = scopeArray[ranNum];            countNum++;            if (countNum == plotNum)                break;            }            countNum = 0;        }        public void createdPlot() {            scopeArray = new int[plotNum];            for (int i = 0; i < plotNum; i++) {                scopeArray[i] = i + 1;        }    }}结果如下: