Hi Everyone,
New here and picking up Java little by little. I just finished writing a SuperLotto program and it works fine, but feel like the code can be improve upon. Any suggestions, tips, etc... on what I can do to improve on my code would be appreciated!
I tried using a standard array,
int ballNumbers = new int[6];
since I know I don't need anything more then 6 balls #, but when I did that, I couldn't use ballNumber.contains().
Thanks in advance!
import java.util.*; class Lotto { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random lottoBall = new Random(); int play, // # of quick pick ticket player wants to purchase count=0, // once count == play, exit program number, // variable to hold the random number between 1-47 ballCount=0; // SuperLotto has 6 balls total + Mega Number ArrayList<Integer> ballNumbers = new ArrayList<Integer>(); // array to hold each ball # System.out.print("Enter # of plays: "); play = input.nextInt(); while(count<play) { while(ballCount < 6) { number = 1+lottoBall.nextInt(47); // check for duplicate numbers if(!ballNumbers.contains(number)) { ballNumbers.add(number); ballCount++; } } // sort array Collections.sort(ballNumbers); // display array content for(int i=0; i<ballNumbers.size();i++) System.out.print(ballNumbers.get(i) + " "); // clear array ballNumbers.clear(); // display Mega Number ball # System.out.printf("\nMega Number: %d\n\n", 1+lottoBall.nextInt(27)); ballCount = 0; count++; } } }