java 彩票中奖程序

  1 import java.io.*;
  2 import java.util.Scanner;
  3 import java.util.Random;
  4 public class Lottery {
  5     public int[] Ran(int x,int z,int y,int Is_repeat){//产生z到y的x个随机数,
  6         //Is_repeat为0时随机数可以重复,1时不能重复
  7         System.out.print("中奖号:");
  8         int[] R=new int[x];
  9         Random RR=new Random();
 10         for(int i=0;i<x;i++){
 11             R[i]=RR.nextInt(y-1)+z;
 12             while(Is_repeat==1&&Exist(R[i],R,i)!=-1)
 13                 R[i]=RR.nextInt(y)+1;//若随机数重复,就重新生成
 14             System.out.print(R[i]+"\t");
 15         }
 16         return R;
 17     };
 18     public int Exist(int x,int r[],int number_r){//数字x是否存在于r
 19         for(int i=0;i<number_r;i++){
 20             if(x==r[i])
 21                 return i;
 22         }
 23         return -1;
 24     }
 25     public int Ncount(int[] a,int number_a,int r[],int number_r,int queue){
 26         //两个数组之间有几个相同的数,
 27         //数目为number_a的a数组在数目为number_r的r数组有几个相同的数,若queue=1时
 28         //返回的数为有连续相同的位数的个数,为0时则不用连续
 29         System.out.println();
 30         System.out.print("选择号:");
 31         int count=0;
 32         if(queue==0){
 33             for(int i=0;i<number_a;i++){
 34                 System.out.print(a[i]+"\t");
 35                 if(new Lottery().Exist(a[i], r, number_r)!=-1)
 36                     count++;
 37             }
 38         }
 39         else{
 40             int teap=0;
 41             for(int i=0;i<number_a-1;i++){
 42                 System.out.print(a[i]+"\t");
 43                 int s=new Lottery().Exist(a[i], r, number_r);
 44                 if(s!=-1){
 45                     teap++;
 46                     if(s<number_a-1&&a[i+1]==r[s+1]){
 47                         continue;
 48                     }
 49                     else{
 50                         if(teap>count){
 51                             count=teap;
 52                             teap=0;
 53                             }
 54                         }
 55                     }
 56                 }
 57             System.out.print(a[number_a-1]+"\t");
 58         }
 59         return count;
 60     }
 61 
 62     public static void main(String[]args){
 63         String game1="玩法1(21选5):输入五个不重复的数(从1到21选)";
 64         System.out.println(game1);
 65         String game2="玩法2(6+1):输入7个数字,最后一位特别号(从0到9选)";
 66         System.out.println(game2);
 67         System.out.print("请问您选择哪种玩法(1/2):");
 68         //BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
 69         Scanner idata=new Scanner(System.in);
 70         int s=idata.nextInt();
 71         try{
 72             //s=in.readLine();
 73             if(s==1||s==2){
 74                 if(s==1){
 75                     System.out.println("请输入五个不重复的数:");
 76                     int[] a=new int[5];
 77                     a[0]=idata.nextInt();
 78                     a[1]=idata.nextInt();
 79                     a[2]=idata.nextInt();
 80                     a[3]=idata.nextInt();
 81                     a[4]=idata.nextInt();
 82                     int[] ran=new Lottery().Ran(5,1,21,1);
 83                     int count0=new Lottery().Ncount(a,5,ran,5,0);
 84                     System.out.println();
 85                     System.out.println("count0="+count0);
 86                     switch(count0){
 87                     case 0:
 88                     case 1:
 89                     case 2:
 90                         System.out.println("未得奖,相同个数为——"+count0);
 91                         break;
 92                     case 3:
 93                         System.out.println("三等奖,相同个数为——"+count0);
 94                         break;
 95                     case 4:
 96                         System.out.println("二等奖,相同个数为——"+count0);
 97                         break;
 98                     case 5:
 99                         System.out.println("一等奖,相同个数为——"+count0);
100                         break;
101                     }
102                 }
103                 else{
104                     System.out.println("请输入七个数:");
105                     int[] a=new int[7];
106                     a[0]=idata.nextInt();
107                     a[1]=idata.nextInt();
108                     a[2]=idata.nextInt();
109                     a[3]=idata.nextInt();
110                     a[4]=idata.nextInt();
111                     a[5]=idata.nextInt();
112                     a[6]=idata.nextInt();
113                     int[] ran=new Lottery().Ran(7,0,10,0);
114                     int count0=new Lottery().Ncount(a,7,ran,7,1);
115                     System.out.println();
116                     System.out.println("count0="+count0);
117                     switch(count0){
118                     case 0:
119                     case 1:
120                     case 2:
121                         System.out.println("未得奖,连续相同个数为——"+count0);
122                         break;
123                     case 3:
124                         System.out.println("四等奖,连续相同个数为——"+count0);
125                         break;
126                     case 4:
127                         System.out.println("三等奖,连续相同个数为——"+count0);
128                         break;
129                     case 5:
130                         System.out.println("二等奖,连续相同个数为——"+count0);
131                         break;
132                     case 6:
133                         System.out.println("一等奖,连续相同个数为——"+count0);
134                         break;
135                     case 7:
136                         System.out.println("特等奖,连续相同个数为——"+count0);
137                         break;
138                         }
139                 }
140                 
141             }else 
142                 System.out.println("输入有误");
143         }catch(Exception e){System.out.println(e);};
144         
145     }
146 }

 

posted @ 2016-09-19 02:11  天下岂有长生不灭者  阅读(2358)  评论(0编辑  收藏  举报