Results 1 to 4 of 4
  1. #1
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Lottery Application

    Hello guys,

    I have created a basic working java lottery application, it predicts 5 numbers and a bonus ball, all working and a great first Java project. However, it will repeat intergers, so i need to add a bit to the code so if a ball has already been picked it wont be picked again....

    Here is the code so far:

    Java Code:
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.net.*;
    
    public class LoteryApp extends Object
    {
    	public static void main(String[] argStrings) throws Exception
    	{
    		Random random = new Random();
    		
    		int[] lotteryNumbers = new int[5];
    		
    		int bonusBall = random.nextInt(49);
    
    		for (int index = 0; index < lotteryNumbers.length; index = index + 1)
    		{
    			lotteryNumbers[index] = random.nextInt(49);
    		}
    		System.out.println("Your lucky numbers are:");
    		for (int index = 0; index < lotteryNumbers.length; index = index + 1)
    		{
    			System.out.println(lotteryNumbers[index]);
    		}
    		System.out.println("Your Bonus Ball is:");
    		System.out.println(bonusBall);
    
    	}
    }
    You have a great forums here, thanks for helping.

    Joel

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Check the code below. isValidNumber(...) method will check if the random number is previously selected or not and will return true if it is not selected before. You can fill that method yoursef.

    Java Code:
    for (int index = 0; index < lotteryNumbers.length; index = index + 1)
    {
        int tmp = random.nextInt(49);
        
        while (!isValidNumber(tmp)) {
    
             tmp = random.nextInt(49);
        }
    
        lotteryNumbers[index] = tmp;
    }
    
    private boolean isValidNumber(int[] lotteryNumbers, int currentIndex, int currentNumber) {
    
    ...
    
    }

  3. #3
    notnumber6 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Hey JavaBean,

    Really like the code, have changed the variable names so i hope you can still understand it but the method where ... is, what shall i put here?

    Java Code:
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import java.net.*;
    
    public class LoteryApp extends Object
    {
    	public static void main(String[] argStrings) throws Exception
    	{
    		Random random = new Random();
    		
    		int[] lotteryNumbers = new int[5];
    		
    		int bonusBall = random.nextInt(49);
    
    		for (int index = 0; index < lotteryNumbers.length; index = index + 1)
    		{
    			while (!hasNotBeingPicked(beforeCheck)) 
    			{
    				beforeCheck = random.nextInt(49);
    			}
    			lotteryNumbers[index] = beforeCheck;
    		}
    		private boolean hasNotBeingPicked(int[] lotteryNumbers, int currentIndex, int currentNumber) 
    		{
    		...
    		}
    		System.out.println("Your lucky numbers are:");
    		
    		for (int index = 0; index < lotteryNumbers.length; index = index + 1)
    		{
    			System.out.println(lotteryNumbers[index]);
    		}
    		
    		System.out.println("Your Bonus Ball is:");
    		
    		System.out.println(bonusBall);
    
    	}
    }

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Java Code:
    private boolean hasNotBeingPicked(int[] lotteryNumbers, int currentIndex, int currentNumber) 
    {
    
        boolean hasNotBeingPicked = true;
    
        for (int i=0; i<currentIndex; i++) {
    
            if (lotteryNumbers[i] == currentNumber) hasNotBeingPicked = false;
        }		
    
        return hasNotBeingPicked;
    }

Similar Threads

  1. JSP - Application object example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 08:40 AM
  2. Replies: 0
    Last Post: 02-16-2008, 10:31 PM
  3. Replies: 0
    Last Post: 02-16-2008, 10:29 PM
  4. how to use JTA for application
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2007, 05:34 PM
  5. Help, GUI Application
    By Felissa in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •