Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-02-2007, 03:40 PM
Member
 
Join Date: Nov 2007
Posts: 7
notnumber6 is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-02-2007, 04:07 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.

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) { ... }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-02-2007, 11:14 PM
Member
 
Join Date: Nov 2007
Posts: 7
notnumber6 is on a distinguished road
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?

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); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-03-2007, 01:42 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP - Application object example Java Tip Java Tips 0 03-17-2008 09:40 AM
Launching an application from another application dynamically Java Tip Java Tips 0 02-16-2008 11:31 PM
Launching an application from another application using thread Java Tip Java Tips 0 02-16-2008 11:29 PM
how to use JTA for application mary Advanced Java 1 07-13-2007 06:34 PM
Help, GUI Application Felissa AWT / Swing 2 07-04-2007 10:19 AM


All times are GMT +3. The time now is 11:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org