1. 程式人生 > >Java實現統計福彩雙色球出現次數(毫無技術可言)

Java實現統計福彩雙色球出現次數(毫無技術可言)

今天早上閒來無事,恰好在看微博的時候看到的雙色球的開獎視訊,沒出現一個號,主持人就會報出現了多少次,於是自己就無聊的寫了一下統計每期的號碼出現的次數程式。

彩票號碼資料來自網上:


程式碼如下Money.java:

package com.heynine.money;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Money {
	
	private List<Map<String, String>> mOldDatas;
	
	/**
	 * 初始化資料
	 * @throws Exception 
	 */
	private void initData() throws Exception {
		mOldDatas = new ArrayList<Map<String, String>>();
		File dataFile = new File("datas.txt");
		BufferedReader reader = new BufferedReader(new FileReader(dataFile));
		String line = null;
		while((line = reader.readLine()) != null) {
			Map<String, String> map = new HashMap<String, String>();
			//得到時間
			String date = line.substring(0, 10);
			map.put("date", date);
			//取得第幾期
			String number = line.substring(11, 18);
			map.put("number", number);
			//紅色號碼
			String red = line.substring(20, 37);
			map.put("red", red);
			//藍色號碼
			String blue = line.substring(38, 40);
			map.put("blue", blue);
			mOldDatas.add(map);
		}
		reader.close();
	}
	
	/**
	 * 統計資料
	 */
	private void count() {
		for (int i = 0; i < mOldDatas.size(); i++) {
			Map<String, String> mapNow = mOldDatas.get(i);
			String[] redNow = mapNow.get("red").split(" ");
			String blueNow = mapNow.get("blue");
			//藍色出現的次數包括現在的次數,所以初始化為1
			int countBlue = 1;  
			int[] countRed = new int[redNow.length];
//			//初始化紅色號碼統計次數為1
			for (int n = 0; n < countRed.length; n++) {
				countRed[n] = 1;
			}
			
			//統計紅色號碼次數
			String strCountRed = "";
			for (int m = 0; m < redNow.length; m++) {
				for (int j = i + 1; j < mOldDatas.size(); j++) {
					Map<String, String> mapOld = mOldDatas.get(j);
					String redOld = mapOld.get("red");
					if (redOld.contains(redNow[m])) {
						countRed[m]++;
					}
				}
				// 對齊處理
				if (m == redNow.length - 1) {
					if (countRed[m] < 10) {
						strCountRed = strCountRed + countRed[m] + " ";
					} else {
						strCountRed = strCountRed + countRed[m];
					}
				} else {
					if (countRed[m] < 10) {
						strCountRed = strCountRed + countRed[m] + "  ";
					} else {
						strCountRed = strCountRed + countRed[m] + " ";
					}
				}
				
			}
			// 儲存
			mapNow.put("countred", strCountRed);
			
			//統計藍色號碼次數
			for (int j = i + 1; j < mOldDatas.size(); j++) {
				Map<String, String> mapOld = mOldDatas.get(j);
				String blueOld = mapOld.get("blue");
				if (blueNow.equals(blueOld)) {
					countBlue++;
				}
			}
			mapNow.put("countblue", countBlue + "");
		}
	}
	
	/**
	 * 資料資料
	 * @throws Exception
	 */
	private void output() throws Exception {
		System.out.println("開始統計");
		FileOutputStream out = new FileOutputStream(new File("out.txt"));
		PrintStream p=new PrintStream(out);
		for (int i =0; i < mOldDatas.size(); i++) {
			Map<String, String> map = mOldDatas.get(i);
			System.out.println("正在統計第 " + map.get("number") + "期 ...");
			p.println(map.get("date") + "   " + map.get("number") + "期");
			p.println("號碼:" + map.get("red") + "  " + map.get("blue"));
			p.println("次數:" + map.get("countred") + "  " + map.get("countblue"));
			p.println();
		}
		p.close();
		out.close();
		System.out.println("統計完成!");
	}

	public static void main(String[] args) {
		Money money = new Money();
		try {
			money.initData();
			money.count();
			money.output();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

執行結果:

       

輸出到文字結果: