0

Hello,
I have written a small java program that simulates a Lottery draw. I would like to generate multiple "lotto tickets" e.g 20 tickets containing 7 numbers at one time when i run the program rather than just a single ticket every time i run the program.
Below is the code snippet for the program. Thanks for your help.

import java.util.ArrayList;
import java.util.List;

public class EuroMillions
{
	public static void main(String[] args)
	{
		int itemAdded,itemAdded1;
		ArrayList<Integer> lottoNbrList = new ArrayList<Integer>(5);
		ArrayList<Integer> lottoNbrList1 = new ArrayList<Integer>(2);
		System.out.println();
		System.out.println("\t\t\tTODAY'S EUROMILLIONS NUMBERS");
        System.out.println();
        System.out.println("\tSelect 5 numbers from 1 to 50\t\tSelect 2 numbers from 1 to 9");
       System.out.println();     
        
		for (itemAdded = 0; itemAdded < 5; )
		{
			int j = (int) (Math.random() * 50) + 1;
			if ((true == lottoNbrList.isEmpty()))
			{
				lottoNbrList.add(j);
				itemAdded += 1;
			}
			else if ( ! lottoNbrList.contains(j))
			{
				lottoNbrList.add(j);
				itemAdded += 1;
			}
		}// end for

		for (int k : lottoNbrList) 
		{
			System.out.print("\t" + k);
		}
		System.out.print("\t");
		
		for (itemAdded1 = 0; itemAdded1 < 2; )
		{
			int i = (int) (Math.random() * 9) + 1;
			if ((true == lottoNbrList1.isEmpty()))
			{
				lottoNbrList1.add(i);
				itemAdded1 += 1;
			}
			else if ( ! lottoNbrList1.contains(i))
			{
				lottoNbrList1.add(i);
				itemAdded1 += 1;
			}
		}// end for

		for (int a : lottoNbrList1) 
		{
			System.out.print("\t" + a);
		}
		System.out.println("\n");
		
		
	}// end main
}// end class
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by quuba
0

make most external for-loop
//
if ((true == lottoNbrList.isEmpty())) True is always true
if ((lottoNbrList.isEmpty())) Means same
//
In this case you can bypass the check lottoNbrList1.isEmpty()
L20-29 new form

if (!lottoNbrList.contains(j)) {
                    lottoNbrList.add(j);
                    itemAdded += 1;
                }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.