首頁 » 原創 » 正文

[原創]使用java生成大量隨機數據存盤的demo

最近學校有個比賽,需要使用大量的隨機數據,我雖然沒參加,不過嘗試編寫了一個批量的成績生成器
//因為時間(懶)的原因,沒寫異常處理模塊,喵~
//隨機中文姓名可用int到char的強制轉換,或另寫個函數,
//將姓和名保存在兩個數組中隨機配對

import java.io.*;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		double dataNumber, fileNumber;
		String disk = null, frontNumber, item1, item2, item3;
		System.out.println("===JAVA成績生成器===");
		System.out.println("===請勿輸入過大的數===");
		TestBufferedWriter tbw = new TestBufferedWriter();
		boolean check = false;

		while (!check) {

			System.out.println("請輸入保存的盤符(單字母):");
			disk = sc.next();
			check = tbw.listSysRoot(disk);
		}

		System.out.println("請輸入文件個數:");
		fileNumber = sc.nextDouble();
		System.out.println("請輸入每個文件數據個數:");
		dataNumber = sc.nextDouble();
		System.out.println("請輸入學號前綴:");
		frontNumber = sc.next();
		System.out.println("請輸入科目1:");
		item1 = sc.next() + ":";
		System.out.println("請輸入科目2:");
		item2 = sc.next() + ":";
		System.out.println("請輸入科目3:");
		item3 = sc.next() + ":";
		
		for (int j = 1; j <= fileNumber; j++) {
			String content = "";

			String temp[] = new String[(int) dataNumber];
			for (int i = 1; i <= (int) dataNumber - 1; i++) {
				System.out.println("正在創建第[" + j + "]"
						+ "個文件的第[" + i + "]個數據!");
				temp[i] = temp[i - 1] + "學號: " + frontNumber + 
						j + i + (int) (Math.random() * 10000) + item1
						+ (int) (Math.random() * 100) + item2 + 
						(int) (Math.random() * 100) + item3
						+ (int) (Math.random() * 100) + "#  \n";
			}
			content = temp[(int) dataNumber - 2];
			String Filename = disk + "://" + "test" + j + ".txt";
			FileOutputStream out = new FileOutputStream(Filename);
			out.write(content.getBytes());
			out.close();
		}
	}

	public boolean listSysRoot(String disk) {
		File[] roots = File.listRoots();
		int i;
		for (i = 0; i < roots.length; i++) {
			if ((disk.toUpperCase() + ":\\").
					equals(roots[i].toString()))
				return true;
		}
		System.out.print("您輸入有誤,可用的盤符有:");
		for (i = 0; i < roots.length; i++) {
			System.out.print(roots[i] + " ");
		}
		return false;
	}

}

發表評論