Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- public class ThreeFourFiveLotto {
- /**
- * Program: ThreeFourFiveLotto.java
- * Purpose: Lotto program uses ArrayList, chooses 3, 4 or 5 numbers
- * Creator: Chris Clarke
- * Created: 27.08.2015
- */
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- ArrayList<Integer> arl = new ArrayList<Integer>();
- int selection;
- int numBalls;
- System.out.print("How many balls to draw (3, 4 or 5)? ");
- do {
- numBalls = in.nextInt();
- if (numBalls<3 || numBalls>5) System.out.print("Error! Must be 3, 4 or 5: ");
- } while (numBalls<3 || numBalls>5);
- int[] chosen = new int[numBalls];
- // initialise the array list
- for (int index=0; index<HIGHEST_NUMBER; index++) {
- arl.add(new Integer(index+1));
- }
- // create random object
- Random ran = new Random();
- for (int i=0; i<numBalls; i++) {
- selection = ran.nextInt(arl.size());
- chosen[i] = arl.get(selection);
- // remove from ArrayList so not chosen again
- arl.remove(selection);
- }
- // sort chosen numbers into ascending order
- Arrays.sort(chosen);
- System.out.println("Here are your random lotto numbers: ");
- // enhanced for loop
- for (int i: chosen) {
- System.out.print(i + " ");
- }
- }
- private static final int HIGHEST_NUMBER = 49; // change this if necessary
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement