Advertisement
brilliant_moves

ThreeFourFiveLotto.java

Aug 27th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.41 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class ThreeFourFiveLotto {
  7.  
  8.     /**
  9.     *   Program: ThreeFourFiveLotto.java
  10.     *   Purpose: Lotto program uses ArrayList, chooses 3, 4 or 5 numbers
  11.     *   Creator: Chris Clarke
  12.     *   Created: 27.08.2015
  13.     */
  14.  
  15.     public static void main(String[] args) {
  16.         Scanner in = new Scanner(System.in);
  17.         ArrayList<Integer> arl = new ArrayList<Integer>();
  18.         int selection;
  19.         int numBalls;
  20.  
  21.         System.out.print("How many balls to draw (3, 4 or 5)? ");
  22.         do {
  23.             numBalls = in.nextInt();
  24.             if (numBalls<3 || numBalls>5) System.out.print("Error! Must be 3, 4 or 5: ");
  25.         } while (numBalls<3 || numBalls>5);
  26.  
  27.         int[] chosen = new int[numBalls];
  28.  
  29.         // initialise the array list
  30.         for (int index=0; index<HIGHEST_NUMBER; index++) {
  31.             arl.add(new Integer(index+1));
  32.         }
  33.  
  34.         // create random object
  35.         Random ran = new Random();
  36.  
  37.         for (int i=0; i<numBalls; i++) {
  38.             selection = ran.nextInt(arl.size());
  39.             chosen[i] = arl.get(selection);
  40.             // remove from ArrayList so not chosen again
  41.             arl.remove(selection);
  42.         }
  43.  
  44.         // sort chosen numbers into ascending order
  45.         Arrays.sort(chosen);
  46.  
  47.         System.out.println("Here are your random lotto numbers: ");
  48.  
  49.         // enhanced for loop
  50.         for (int i: chosen) {
  51.             System.out.print(i + " ");
  52.         }
  53.     }
  54.  
  55.     private static final int HIGHEST_NUMBER = 49; // change this if necessary
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement