Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lu Liu
- * 11/30/2015
- * CSCI-111-D01
- * Generate Lucky For Life Lottery Numbers
- */
- package chapter07;
- import java.util.Random;
- import java.util.Scanner;
- public class LotteryNumberGenerator {
- public static void main(String[] args) {
- final String TITLE = "Lottery Number Generator";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- getGroupNumbers(5);
- getBallNumber();
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // Sort Group Numbers
- public static void selectionSort(int[] list) {
- for (int i = 0; i < list.length - 1; i++) {
- int currentMin = list[i];
- int currentMinIndex = i;
- for (int j = i + 1; j < list.length; j++) {
- if (currentMin > list[j]) {
- currentMin = list[j];
- currentMinIndex = j;
- }
- }
- if (currentMinIndex != i) {
- list[currentMinIndex] = list[i];
- list[i] = currentMin;
- }
- }
- }
- // Get Group Numbers And Print Out
- public static int[] getGroupNumbers(int n) {
- int[] lottery = new int[n];
- int[] chooseFrom = new int[43];
- for (int i = 1; i <= 43; i++)
- chooseFrom[i - 1] = i;
- Random rand = new Random();
- int N = 43;
- int index;
- for (int i = 0; i < n; i++) {
- index = rand.nextInt(N);
- lottery[i] = chooseFrom[index];
- chooseFrom[index] = chooseFrom[N - 1];
- N--;
- }
- selectionSort(lottery);
- System.out.print("Your Lucky For Life Cash numbers are:");
- for (int i = 0; i < n; i++)
- System.out.printf("%2d ", lottery[i]);
- System.out.println();
- return lottery;
- }
- // Get Ball Number And Print Out
- public static void getBallNumber() {
- System.out.println("Your Lucky Ball Number is: " + ((int) (Math.random() * 43) + 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement