/**
*@ the titlt about a Random example about choose 7 from 33
*@ the auther Nicky (EN) QuQiang(CH)
*@ the date 2006.9.1
**/
/** the rules
//一等獎:選中6個正選號及特別號;
//二等獎:選中5個正選號及特別號;
//三等獎:選中5個正選號;
//四等獎:選中4個正選號及特別號;
//五等獎:選中4個正選號或選中3個正選號及特別號;
//六等獎:選中3個正選號。
**/
import java.util.*;
public class NotSameRandoml{
private static String transform;
private static String match="00";
private static int special;
//產(chǎn)生彩票主邏輯函數(shù)
private static void Nicky(int[] guess){
Random r = new Random(); //構(gòu)造偽隨機生成器
//某些映射實現(xiàn)可明確保證其順序,如 TreeMap 類;某些映射實現(xiàn)則不保證順序,如 HashMap 類
Map map = new TreeMap(); //Map 接口的實現(xiàn)
int n = 0;
int nt = 1;
String[] temps=new String[7];
while(true){
n = r.nextInt(33)+1; //產(chǎn)生1~33的隨機數(shù)
//if( map.get(new Integer(n))!=null){
// nt = ((Integer)map.get(new Integer(n))).intValue();
//}
//避免了產(chǎn)生的隨機數(shù)字重復(fù)
if(map.containsValue(n)){
continue;
}
map.put(new Integer(nt),new Integer(n));//將指定的值與此映射中的指定鍵相關(guān)聯(lián)
if(map.size()==7){
break;
}
nt++;
}
Iterator it = map.keySet().iterator(); //返回此映射中包含的鍵的 set 視圖
for(int i=0;it.hasNext();i++){
Object o = it.next();
// 為了更符合現(xiàn)實中33選7,數(shù)字為01。。。2位
int temp=((Integer)map.get(o)).intValue();
if(temp>=1&&temp<10){
transform="0"+Integer.toString(temp);
match=match+" "+transform;
temps[i]=transform;
if(((Integer)o).intValue()==7){
special=temp;
System.out.println(""+transform+"為產(chǎn)生的特別中獎中獎號碼");
}else
System.out.println(""+transform+"為產(chǎn)生的第"+((Integer)o).intValue()+"個中獎號碼");
}else{
temps[i]=Integer.toString(temp);
match=match+" "+temps[i];
if(((Integer)o).intValue()==7){
System.out.println(""+transform+"為產(chǎn)生的特別中獎中獎號碼");
}else
System.out.println(""+temp+"為產(chǎn)生的第"+((Integer)o).intValue()+"個中獎號碼");
}
}
String creat=match.substring(3);
System.out.println("所產(chǎn)生的中獎號碼串為:"+creat);
//System.out.println("對產(chǎn)生的中獎號碼順序排序為:"+creats);
Sort(temps);
check(map,guess);
}
//實現(xiàn)排序,也可以調(diào)用方法,但是卻必須要解決Void問題
private static void Sort(String[] temps) {
for(int i=0;i<temps.length;i++){
for(int j=i+1;j<temps.length;j++){
if(Integer.parseInt(temps[i])>Integer.parseInt(temps[j])){
String k;
k=temps[i];temps[i]=temps[j];temps[j]=k;
}
}
}
System.out.println("對產(chǎn)生的中獎號碼順序排序為:");
for(int i=0;i<temps.length;i++){
System.out.print(temps[i]+" ");
}
System.out.println("\n");
}
//輸出結(jié)果類別
private static void check(Map map ,int[] guess){
int flag=0;
for(int i=0;i<guess.length-1;i++){
if(map.containsValue(guess[i])){
flag++;
}
}
if(guess[guess.length-1]==special){
flag=flag+10;
}
switch(flag){
case 16: System.out.println("恭喜您中一等獎");break;
case 15: System.out.println("恭喜您中二等獎");break;
case 5: System.out.println("恭喜您中三等獎");break;
case 14: System.out.println("恭喜您中四等獎");break;
case 13: System.out.println("恭喜您中五等獎");break;
case 4: System.out.println("恭喜您中五等獎");break;
case 3: System.out.println("恭喜您中六等獎");break;
default: System.out.println("謝謝參與,祝您下次中獎");
}
}
//說明
private static void usage(){
System.out.println("Usage:java Randomol program [the number you guess for the lucky nums.]");
System.out.println("\t And the nums. you must typed 7,else you will be cancel by the game rules");
System.out.println("\t The first 6 nums is your basic nums.,the last one is your special num.");
System.exit(0);
}
//主函數(shù)
public static void main(String []args){
if(args.length==0||args.length>7){
usage();
}//帶入?yún)?shù)
int[] guess=new int[7];
for(int i=0;i<args.length;i++){
guess[i]=Integer.parseInt(args[i]);
}
//判斷所輸入的號碼是否相同
List <Integer> ls= new ArrayList<Integer>();
for(int i=0;i<guess.length;i++){
if(ls.contains(guess[i])){
System.out.println("您所買的號碼不可以相同");
System.exit(0);
}else ls.add(guess[i]);
}
Nicky(guess);
System.exit(0);
}
}