當前位置:編程學習大全網 - 圖片素材 - JAVA課程設計 彩票號碼產生器

JAVA課程設計 彩票號碼產生器

我這有個超級大樂透的代碼在控制臺輸出的 還沒有統計獎項概率 希望可以幫到妳 :1.先說下思路 a.聲明2個數組和2個變量數組分別放前區的35個數和後區的12個數,變量分別為這兩個數組的長度 b.首先隨機獲得前區的5個號碼:Random .nextInt(前區長度的變量) 獲得隨機數,找到第壹個數組中下標為這個隨機數的元素,取出放在變量中. 這   兒可以判斷格式 的.可以用正則表達式判斷是否為各位數,如果是的話就補全0.然後移除數組中這個元素.當然存儲數組的變量要減去1的然後重復  以上的4次這樣就獲得了1-35中5個不重復的數字. c.同上. 2.附代碼SuperJoyful.javaimport java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Random;

import java.util.regex.Matcher;

import java.util.regex.Pattern;/**

* 以下是模擬壹個超級大樂透隨機選號功能,嚴格按概率生成

* @author Jack

*

*/

public class SuperJoyful {

// 聲明壹個List用於存放前區號碼

private List<Integer> objFrontList = new ArrayList<Integer>();

// 聲明壹個List用於存放後區號碼

private List<Integer> objBackList = new ArrayList<Integer>();

// 聲明壹個正則表達式對象,用於匹配是否是壹位數字,用於輸出時驗證

Pattern objP = Pattern.compile("\\d");

// 所要操作的字符串

Matcher objM = null;

String[] Front = new String[5];

String[] Back = new String[2]; // 初始化搖獎號碼

public void init() {

for (int i = 1; i <= 35; i++) {

objFrontList.add(i);

}

for (int i = 1; i <= 12; i++) {

objBackList.add(i);

}

} // 開始搖獎

public void beginLottery() {

Random objRandom = new Random();

int nFrontCount = 35; // 前區號碼總數

int nBackCount = 12; // 後區號碼總數 // 搖獎前先清空LIST,再初始化

objFrontList.clear();

//System.out.println(objFrontList);

objBackList.clear();

//System.out.println(objBackList);

this.init();

/**

* 產生5個前區號碼

*/

for (int i = 0; i < 5; i++) {

//System.out.println("nFrontCount:"+nFrontCount);

// 初始時有35個前區號,隨機產生壹個索引

int nIndex = objRandom.nextInt(nFrontCount);

// 將選出的號碼暫時存放在變量中,帶正則表達式驗證

int nTemp = objFrontList.get(nIndex);

String strTemp = new Integer(nTemp).toString();

// 將獲得的號碼與正則表達式匹配

objM = objP.matcher(strTemp);

boolean flag = objM.matches();

// 如果是壹位數,則在前面補零

if (flag) {

Front[i] = ("0" + strTemp + " ");

} else {

Front[i] = (strTemp + " ");

}

// 刪除LIST中該索引處的號碼,因為選出壹個就不再放回

objFrontList.remove(nIndex);

// 號碼總數減少壹個

nFrontCount--;

}

Arrays.sort(Front);

for (int n = 0; n < Front.length; n++) {

System.out.print(Front[n] + "\t");

}

System.out.print("+ ");

/**

* 產生2個後區號碼

*/

for (int i = 0; i < 2; i++) {

//System.out.println("nBackCount:"+nBackCount);

// 初始時有12個後區號,隨機產生壹個索引

int nIndex = objRandom.nextInt(nBackCount);

// 將選出的號碼暫時存放在變量中,帶正則表達式驗證

int nTemp = objBackList.get(nIndex);

String strTemp = new Integer(nTemp).toString();

// 將獲得的號碼與正則表達式匹配

objM = objP.matcher(strTemp);

boolean flag = objM.matches();

// 如果是壹位數,則在前面補零

if (flag) {

Back[i] = ("0" + strTemp + " ");

} else {

Back[i] = (strTemp + " ");

}

// 刪除LIST中該索引處的號碼,因為選出壹個就不再放回

objBackList.remove(nIndex);

// for(int n = 0; n<objBackList.size();n++){

// System.out.println("objBackList:"+objBackList.get( n ));

// }

// 號碼總數減少壹個

nBackCount--;

}

Arrays.sort(Back);

for (int n = 0; n < Back.length; n++) {

System.out.print(Back[n] + "\t");

}

// 產生壹註後回車

System.out.println("");

} // 按要求輸出多少註彩票

public void outPutLottery(int vnCount) {

for (int i = 0; i < vnCount; i++) {

this.beginLottery();

}

} /**

* @param args

*/

public static void main(String[] args) {

SuperJoyful objSJ = new SuperJoyful();

EnterConsole objEC = new EnterConsole();

// 聲明壹個正則表達式對象,用於匹配是否是數字

Pattern objP = Pattern.compile("\\d{1,}");

// 所要操作的字符串

Matcher objM = null;

// 接收控制臺輸入

String objTemp = (String) objEC.printConsole();

//String strTemp = (String)objTemp;

objM = objP.matcher(objTemp);

boolean flag = objM.matches();

int nTemp = 0;

if (flag) {

nTemp = new Integer(objTemp);

} else {

System.out.println("對不起,只能輸入數字!");

}

objSJ.outPutLottery(nTemp);

}

}EnterConsole.javaimport java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class EnterConsole

{

public Object printConsole(){

System.out.print("請輸入妳要隨機選取多少註,確認回車即可:");

BufferedReader objBR = new BufferedReader(new InputStreamReader(System.in));

String strValue = null;

try

{

strValue = (String)objBR.readLine();

}

catch ( IOException e )

{

// TODO Auto-generated catch block

e.printStackTrace();

}

return strValue;

}

}

  • 上一篇:哪位有《大長今》(武大郎版)和粵語版的歌詞
  • 下一篇:椅子在家裏不可或缺,什麽品牌的椅子性價比高?
  • copyright 2024編程學習大全網