Advertisement
luliu

Generate Lucky For Life Lottery Numbers

Nov 30th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. /*
  2.  * Lu Liu
  3.  * 11/30/2015
  4.  * CSCI-111-D01
  5.  * Generate Lucky For Life Lottery Numbers
  6.  */
  7. package chapter07;
  8.  
  9. import java.util.Random;
  10. import java.util.Scanner;
  11.  
  12. public class LotteryNumberGenerator {
  13.  
  14.     public static void main(String[] args) {
  15.         final String TITLE = "Lottery Number Generator";
  16.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  17.         System.out.println("Welcome to " + TITLE);
  18.         Scanner sc = new Scanner(System.in);
  19.         do {
  20.             getGroupNumbers(5);
  21.             getBallNumber();
  22.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  23.         sc.close();
  24.         System.out.println("Thank you for using " + TITLE);
  25.     }
  26.  
  27.     private static boolean doThisAgain(Scanner sc, String prompt) {
  28.         System.out.print(prompt);
  29.         String doOver = sc.nextLine();
  30.         return doOver.equalsIgnoreCase("Y");
  31.     }
  32.  
  33.     // Sort Group Numbers
  34.     public static void selectionSort(int[] list) {
  35.         for (int i = 0; i < list.length - 1; i++) {
  36.             int currentMin = list[i];
  37.             int currentMinIndex = i;
  38.             for (int j = i + 1; j < list.length; j++) {
  39.                 if (currentMin > list[j]) {
  40.                     currentMin = list[j];
  41.                     currentMinIndex = j;
  42.                 }
  43.             }
  44.             if (currentMinIndex != i) {
  45.                 list[currentMinIndex] = list[i];
  46.                 list[i] = currentMin;
  47.             }
  48.         }
  49.     }
  50.  
  51.     // Get Group Numbers And Print Out
  52.     public static int[] getGroupNumbers(int n) {
  53.         int[] lottery = new int[n];
  54.         int[] chooseFrom = new int[43];
  55.         for (int i = 1; i <= 43; i++)
  56.             chooseFrom[i - 1] = i;
  57.         Random rand = new Random();
  58.         int N = 43;
  59.         int index;
  60.         for (int i = 0; i < n; i++) {
  61.             index = rand.nextInt(N);
  62.             lottery[i] = chooseFrom[index];
  63.             chooseFrom[index] = chooseFrom[N - 1];
  64.             N--;
  65.         }
  66.         selectionSort(lottery);
  67.         System.out.print("Your Lucky For Life Cash numbers are:");
  68.         for (int i = 0; i < n; i++)
  69.             System.out.printf("%2d ", lottery[i]);
  70.         System.out.println();
  71.         return lottery;
  72.     }
  73.  
  74.     // Get Ball Number And Print Out
  75.     public static void getBallNumber() {
  76.         System.out.println("Your Lucky Ball Number is: " + ((int) (Math.random() * 43) + 1));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement