posts - 97,  comments - 93,  trackbacks - 0
          /**
              *@ 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;


            //產生彩票主邏輯函數
             private static void Nicky(int[] guess){
                Random r = new Random();     //構造偽隨機生成器
                //某些映射實現可明確保證其順序,如 TreeMap 類;某些映射實現則不保證順序,如 HashMap 類
                Map map = new TreeMap();   //Map 接口的實現
                int n = 0;
                int nt = 1;
                String[] temps=new String[7];
                
                while(true){
                    n = r.nextInt(33)+1; //產生1~33的隨機數
                    //if( map.get(new Integer(n))!=null){
                    //   nt = ((Integer)map.get(new Integer(n))).intValue();  
                    //}
                    //避免了產生的隨機數字重復
                    if(map.containsValue(n)){
                      continue;
                    }
                    map.put(new Integer(nt),new Integer(n));//將指定的值與此映射中的指定鍵相關聯
                    if(map.size()==7){
                       break;
                    }
                    nt++;
                }
                
                Iterator it = map.keySet().iterator(); //返回此映射中包含的鍵的 set 視圖
                for(int i=0;it.hasNext();i++){
                 Object o = it.next();
                  // 為了更符合現實中33選7,數字為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+"為產生的特別中獎中獎號碼");
                         }else  
                           System.out.println(""+transform+"為產生的第"+((Integer)o).intValue()+"個中獎號碼");
                  }else{
                  temps[i]=Integer.toString(temp);
                  match=match+" "+temps[i];
                  if(((Integer)o).intValue()==7){
                     System.out.println(""+transform+"為產生的特別中獎中獎號碼");
                  }else
                  System.out.println(""+temp+"為產生的第"+((Integer)o).intValue()+"個中獎號碼");
                 }
                }
                String creat=match.substring(3);
                System.out.println("所產生的中獎號碼串為:"+creat);
                //System.out.println("對產生的中獎號碼順序排序為:"+creats);
                Sort(temps);
                check(map,guess);
             }
             
             //實現排序,也可以調用方法,但是卻必須要解決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("對產生的中獎號碼順序排序為:");
                for(int i=0;i<temps.length;i++){
                    System.out.print(temps[i]+" ");
                }
                System.out.println("\n");
              }
             
             
            //輸出結果類別  
             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);
             }
             
             
            //主函數
             public static void main(String []args){
                  if(args.length==0||args.length>7){
                        usage();
                      }//帶入參數
                      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);
             }
          }
          posted on 2006-10-12 18:45 wqwqwqwqwq 閱讀(454) 評論(0)  編輯  收藏 所屬分類: Data Structure && Algorithm
          <2006年10月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊

          J2ME技術網站

          java技術相關

          mess

          搜索

          •  

          最新評論

          閱讀排行榜

          校園夢網網絡電話,中國最優秀的網絡電話
          主站蜘蛛池模板: 灯塔市| 开阳县| 巧家县| 兴业县| 新邵县| 青阳县| 鄂州市| 涟源市| 会泽县| 海南省| 阿拉尔市| 五台县| 神池县| 浙江省| 甘肃省| 三台县| 宣化县| 禹州市| 勃利县| 凌云县| 开远市| 如东县| 孟州市| 彭山县| 荔波县| 体育| 桦川县| 宁化县| 东兰县| 德保县| 双峰县| 金溪县| 英德市| 石首市| 洛南县| 石棉县| 勃利县| 道真| 仙桃市| 吉水县| 文安县|