彩票开奖代码

package Lottery;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.StringTokenizer;

public class Lottery {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int playtimes = 0 ;
		try{
			if(args.length == 1){
				playtimes = Integer.parseInt(args[0]);
			} else{
				playtimes = 1;
			}
		} catch(NumberFormatException e){
			System.out.println(e);
		}
		
		while(playtimes > 0){
			int choice = init();
			switch(choice){
			case 1:
				int[] numbers_1 = new int[5];
				input_1(numbers_1);
				check_1(numbers_1);
				break;
			case 2:
				int[] numbers_2 = new int[7];
				input_2(numbers_2);
				check_2(numbers_2);
				break;
			}
			playtimes--;
		}
	}
	
	private static void check_2(int[] numbers_2) {
		// TODO Auto-generated method stub
		
	}

	private static void input_2(int[] numbers_2) {
		// TODO Auto-generated method stub
		
	}

	private static void check_1(int[] numbers_1) {
		// TODO Auto-generated method stub
		System.out.println("21选5的规则");
		int[] award = new int[5];
		Random ran = new Random();
		L3:
			//后台产生5个数,代表中奖的数字
			for(int i = 0 ; i < 5 ; i++){
				//余数要么为0要么为20.不为21,所以要加1
				award[i] = Math.abs(ran.nextInt())%21 + 1;
				for(int j = 0 ; j < i ; j++){
					if(award[j] == award[i]){
						//i--;//不要?
						continue L3;
					}
				}
				
			}
		int[] my = new int[]{1,2,3,4,5};//测试
		int same = 0;
		L4:
			for(int i = 0 ; i < 5 ; i++){
				for(int j = 0 ; j < 5 ; j++){
					if(numbers_1[i] == award[j]){ //测试
						same++;
						//continue L4; //错误?
					}
				}
			}
		switch(same){
		case 3:
			System.out.println("Congratulations on the three prize");
			break;
		case 4:
			System.out.println("Congratulations on the two prize");
			break;
		case 5:
			System.out.println("Congratulations on the first prize");
			break;
			default:
				System.out.println("No winning, good luck next time");
		}
		System.out.println("你选中的号码是:");
		for(int i = 0 ; i < 5 ; i++){
			System.out.print(""+numbers_1[i]);
		}
		System.out.println();
		System.out.println("中奖号码是:");
		for(int i = 0 ; i < 5 ; i++){
			System.out.print(""+award[i]);
		}
		System.out.println();
	}

	private static void input_1(int[] numbers_1) {
		// TODO Auto-generated method stub
		System.out.println("请输入5个数,每个数必须各不相同,且都在1~21之间");
		boolean InputLoopflage = true;
		StringTokenizer st;
		InputStreamReader ir;
		BufferedReader in;
		String s = new String();
		try{
			ir = new InputStreamReader(System.in);
			in = new BufferedReader(ir);
			L1:
				while(InputLoopflage){
					s = in.readLine();//输入5个数,空格分开
					st = new StringTokenizer(s);//相当于string.splite()
					int count = st.countTokens();
					if(count != 5){
						System.out.println("请输入5个(1~21)的数字! please try agin");
						continue;
					}
					try{
						for(int i = 0 ; i < 5 ; i++){
							numbers_1[i] = Integer.parseInt(st.nextToken());
							for(int j = 0 ; j < i ; j++){
								if(numbers_1[i] == numbers_1[j]){
									System.out.println("5个数不能有相同的 please try agin!");
									continue;
								}
							}
						}
						for(int i = 0 ; i < 5 ; i++){
							if(numbers_1[i] < 1 || numbers_1[i] > 21){
								System.out.println("5个数要在1~21之间  please try agin!");
								continue;
							}
						}
					} catch(NumberFormatException e){
						e.printStackTrace();
						System.out.println("please try agin!");
						continue;
					}
					InputLoopflage = false;
				}
			
		} catch(IOException e){
			e.printStackTrace();
		}
	}

	static int init(){
		boolean InputLoopflag = true;
		int n = 0;
		System.out.println("请按数字键1或2选择一种玩法,1代表21选5,2代表6+1");
		InputStreamReader ir;
		BufferedReader in;
		String s = new String();
		try{
			ir = new InputStreamReader(System.in);
			in = new BufferedReader(ir);
			while(InputLoopflag){
				s = in.readLine();
				try{
					n = Integer.parseInt(s);
					if(n!=1 && n!= 2){
						System.out.println("enter error! plearse try agin");
						continue;
					}
					InputLoopflag = false;
				} catch(NumberFormatException e){
					System.out.println(e);
					System.out.println("Not number , Please try agin!");
					continue;
				}
			}
			
		} catch(IOException e){
			e.printStackTrace();
		}
		
		return n;
	}
	
	
	
	

}

 

你可能感兴趣的:(java基础)