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 阅读(437) 评论(0)  编辑  收藏 所属分类: Data Structure && Algorithm

只有注册用户登录后才能发表评论。


网站导航:
 
<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234




常用链接

留言簿(10)

随笔分类(95)

随笔档案(97)

文章档案(10)

相册

J2ME技术网站

java技术相关

mess

搜索

  •  

最新评论

阅读排行榜

校园梦网网络电话,中国最优秀的网络电话