java entry project: two color ball lottery

Posted by muadzir on Thu, 24 Oct 2019 16:14:16 +0200

Title Requirements:
In addition, the red ball number cannot be repeated
Because I'm not familiar with the use of classes, all of them are written into the same class. The impression is that the output and logic business are separated, but the feeling is not so separated, so it seems that the logic is a bit confused, and has not been converted to the object-oriented programming idea. In addition to the main method, six other methods are written.

public static int menu() / / display menu
public static int buyBalls (int[] balls) / / buy lottery tickets, return how many tickets to buy
Public static int [] random balls (Boolean isbuy) / / generate random winning code
public static boolean isRepeat(int[] arr,int key) / / judge duplicate elements
public static int judgePrize(int[] buyBalls,int[] prizeBalls) / / judge whether to win the prize and return the winning level
public static void printPrize(int prize,int num) / / output the winning information

1. The main method uses the method of dead cycle + forced exit.

public static void main(String[] args) {
		boolean flagCase = true;//Whether to quit
		boolean isBuy = false;//Have you bought it?
		int num = 0;//Buy num notes
		int[] balls = new int[7]; 
		while (flagCase) {
			int choose = menu();
			switch (choose) {
			case 1:
				num = buyBalls (balls); 
				isBuy = true; 
				break;
			case 2:
				if (isBuy) {
					int[] prizeBalls = randomBalls(isBuy);//Generate winning code
					int prize = judgePrize(balls, prizeBalls);//Judge the winning grade
					
					System.out.print("The lottery number you bought is:");
					for (int i : balls) {
						System.out.print(i+"\t");
					}
					System.out.print("\n The winning ticket number of the current period is: ");
					for (int i : prizeBalls) {
						System.out.print(i+"\t");
					}
					printPrize(prize, num);
					
				} else {
					System.out.println("You haven't bought the lottery yet. Please buy the lottery first.");
				}
				break;
			case 3: flagCase = false;return;
			default:System.out.println("Input error.");
			}			
		}	
	}

2. public static int menu() / / display menu
There's a problem here. The scanner can't be turned off. A warning is reported. If the scanner is turned off, a warning will be reported.
Exception in thread "main" java.util.NoSuchElementException
Try to close it at the end of main method or in menu.
The reason for the error is that no matter how many last Scanner objects are defined, they share a constant flow. Once closed, they can no longer be used.

@SuppressWarnings("resource")
	public static int menu() {
		System.out.println("*****Welcome to the dichroic lottery system*****");
		System.out.println("\t1,Buying lottery tickets");
		System.out.println("\t2,Check the lottery");
		System.out.println("\t3,Sign out");
		System.out.println("*************************");
		System.out.print("Please select the menu:");
		Scanner scan = new Scanner(System.in);
		int choose = scan.nextInt();
		
		return choose;
	}

3. public static int buyBalls (int[] balls) / / to buy lottery tickets, return how many tickets to buy
Judge first, and input data into the array only when it is legal.

//How many tickets to buy?
	public static int buyBalls (int[] balls ) {
		System.out.println("[Two color ball lottery system > Buy lottery tickets]");
		
		Arrays.fill(balls,0);//Reset array, reset before purchase again
		System.out.println("How many notes do you need? :");
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		
		int red = -1,blue = -1;
		int i = 0;//Record the index of the current array. It will not change if the input is incorrect.
		//Red ball is legal and heavy
		do {
			System.out.println("Six red ball numbers entered<The number is 1.-33>,["+(i+1)+"]The red ball numbers are:");
			red = scan.nextInt();
			if (red>0 && red<34) {
				if (isRepeat(balls,red)) {//Jump out on repeat
					System.out.println("A duplicate number has been entered, please enter the["+(i+1)+"]Value:");
				} else {
					balls[i] = red;
					i++;
				}				
			} else {
				System.out.println("You have entered a number that is out of range. Please enter the["+(i+1)+"]Value:");
			}
		}while(i < 6);
		
		//Basketball is legal
		System.out.println("Please enter a blue ball number<The number is 1.-16>:");
		blue = scan.nextInt();
		if (blue > 0 && blue < 17) {
			balls[6] = blue;
		}
		System.out.print("You buy it altogether."+num+"Note, total payment required["+(2*num)+"]Yuan.");
		System.out.println("The selected number is:"+Arrays.toString(balls));
		return num;
	}

4. Public static int [] random balls (Boolean isbuy) / / generate random winning code
Here, the Math.random() method is used to generate the random number of double type between [0.0-1.0]
For generating random numbers (int) between [m,n] (math. Random() * (n-m + 1) + m);

//Generate random winning code
	public static int[] randomBalls(boolean isBuy) {
		int[] prizeBalls = new int[7];
		if (isBuy) {
			//Generate winning code
			int red = -1;
			int i = 0;//Record the index of the current array. It will not change if the input is incorrect.
			do {
				red = (int)( Math.random()*33+1);//Random number of 1-33
				if (!isRepeat(prizeBalls, red)) {//Assignment if not repeated
					prizeBalls[i] = red;
					i++;
				}
			}while(i < 6);
			prizeBalls[6] = (int)( Math.random()*15+1);//Random number of 1-16
		} else {
			System.out.println("You haven't bought the lottery, please bet:");
		}
		return prizeBalls;
	}

5. public static boolean isRepeat(int[] arr,int key) / / judge duplicate elements

//Judge repeating elements
	public static boolean isRepeat(int[] arr,int key) {
		boolean flag = false;//Default not repeated
		for (int i : arr) {
			if (i == key) {
				flag = true;
				break;
			}
		}
		return flag;
	}

6. public static int judgePrize(int[] buyBalls,int[] prizeBalls) / / judge whether to win, and return to the winning level.
In order not to change the position of the ball number, it is copied to the new array. Arrays.copyOf(int[] original, int newLength) returns a new array. The length is used to specify the length of the new array. If necessary, null is filled.

It's convenient to compare equality. First, sort the array. sort(int[] a, int fromIndex, int toIndex). The array in the specified range is sorted. Here, only the red ball is sorted.

This method of judging the number of repetitions is mentioned in the previous algorithm, but it can't be remembered. I didn't find it on the Internet, even doubted whether it was right or not.

//Judge whether winning or not, return to winning level
	public static int judgePrize(int[] buyBalls,int[] prizeBalls) {
		int[] arr1 = Arrays.copyOf(prizeBalls, prizeBalls.length);
		int[] arr2 = Arrays.copyOf(buyBalls, buyBalls.length);
		
		Arrays.sort(arr1, 0, 7);//Ascending, [0,7)
		Arrays.sort(arr2, 0, 7);//Ascending, [0,7)
		
		int iequal = 0;//Equal number of red balls
		int i1 = 0;//Record the subscript of arr1
		/*
		 * Take the winning code arr1 as a reference and compare it with the value of the purchased code arr2
		 */
		//Red ball only
		for(int i = 0;i < arr2.length-1;i++) {
			for (int j = i1;j < arr1.length-1;j++) {
				
				//If arr2[i] is smaller than arr1[j], then it is smaller than every number after j, so there is no need to compare.
				if (arr2[i] < arr1[j]) {
					break;
				} else if(arr2[i] == arr1[j]) {
					iequal++;
					break;
				} else { 
					//If arr2 [i] > Arr1 [j], then there must be arr2 [i + 1] > Arr1 [j], so j does not need to compare from the beginning, record the subscript position.
					i1 = j;
				}
			}
		}
		boolean blue = arr1[6]==arr2[6];
		int prize = 0;//Record winning grade
		if (iequal == 6 && blue) {
			prize = 1;
		} else if (iequal == 6) {
			prize = 2;
		} else if (iequal == 5 && blue)  {
			prize = 3;
		}  else if (iequal == 5 || (iequal == 4 && blue)) {
			prize = 4;
		} else if (iequal == 4 || (iequal == 3 && blue)){
			prize = 5;
		} else if (blue) {
			prize = 6;
		} else {
			prize = 0;
		}
		return prize;	
	}

7. public static void printPrize(int prize,int num) / / output the winning information.

public static void printPrize(int prize,int num) {
		switch(prize) {
		case 1 :
			System.out.println("\n Congratulations, the first prize of China"+num+"Note investment"+(num*2)+"element");
			break;
		case 2 :
			System.out.println("\n Congratulations, second prize"+num+"Note investment"+(num*2)+"element");
			break;
		case 3 :
			System.out.println("\n Congratulations, third prize"+num+"Note investment"+(num*2)+"element");
			break;
		case 4 :
			System.out.println("\n Congratulations, the fourth prize of the middle school"+num+"Note investment"+(num*2)+"element");
			break;
		case 5 :
			System.out.println("\n Congratulations, the fifth prize"+num+"Note investment"+(num*2)+"element");
			break;
		case 6 :
			System.out.println("\n Congratulations, the sixth prize"+num+"Note investment"+(num*2)+"element");
			break;
		default:
			System.out.println("\n Sorry, you didn't win");
		}
	}

The fifth method is to judge whether there is an equal value between two groups. I don't feel very grateful to have a great God to guide me.

Topics: Programming Java