Results 1 to 4 of 4
Thread: Lottery Application
- 11-02-2007, 01:40 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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:
You have a great forums here, thanks for helping.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); } }
Joel
- 11-02-2007, 02:07 PM #2
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) { ... }
- 11-02-2007, 09:14 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
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); } }
- 11-03-2007, 11:42 AM #4
Similar Threads
-
JSP - Application object example
By Java Tip in forum Java TipReplies: 0Last Post: 03-17-2008, 07:40 AM -
Launching an application from another application dynamically
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:31 PM -
Launching an application from another application using thread
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:29 PM -
how to use JTA for application
By mary in forum Advanced JavaReplies: 1Last Post: 07-13-2007, 04:34 PM -
Help, GUI Application
By Felissa in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 08:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks