| | |
Help with Java programming for lottery
![]() |
Could you use an Else If type thing.
PETA People for the Eating of Tasty Animals.
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
put all lottos numbers in an array and search through it....
use a do-while loop
make sure to initialize your array to something that would be invalidlike 0 to avoid a null pointer exception
use a do-while loop
make sure to initialize your array to something that would be invalidlike 0 to avoid a null pointer exception
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
This is from the Deitel Book. I also post the requirements they need. And to answer your question, look at number 6. 
1. need to add for a "for" statement that will execute the application 4 times.
2. Using a 2 dimensional boolean array (uniqueNumber) . THe value of that variable in the array should be set to false. Add a nested "for" statement to set the 40 values to 'false'. I think it requires a for statement to index the array.
3. Init a String to hold selections.
4. Iterating over 5 numbers selected. Each time this statement executes, a single unique lottery value will be generated.
5. a do...while statment. Using a method "generate" pass it to args 0-39. To return a random number.
6. Add code to set the value in uniqueNumber represented by the selected number to true. It won't be selected again.
7. then it's supposed to display.
Code tags added. -Narue

1. need to add for a "for" statement that will execute the application 4 times.
2. Using a 2 dimensional boolean array (uniqueNumber) . THe value of that variable in the array should be set to false. Add a nested "for" statement to set the 40 values to 'false'. I think it requires a for statement to index the array.
3. Init a String to hold selections.
4. Iterating over 5 numbers selected. Each time this statement executes, a single unique lottery value will be generated.
5. a do...while statment. Using a method "generate" pass it to args 0-39. To return a random number.
6. Add code to set the value in uniqueNumber represented by the selected number to true. It won't be selected again.
7. then it's supposed to display.
Java Syntax (Toggle Plain Text)
// This application picks randomly generated numbers for a lottery. import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.util.Random; import javax.swing.*; public class LotteryPicker extends JFrame { // JLabel and JTextField for first lottery private JLabel oneJLabel; private JTextField oneJTextField; // JLabel and JTextField for second lottery private JLabel twoJLabel; private JTextField twoJTextField; // JLabel and JTextField for third lottery private JLabel threeJLabel; private JTextField threeJTextField; // JLabel and JTextField for fourth lottery private JLabel fourJLabel; private JTextField fourJTextField; // JButton to generate lottery numbers private JButton generateJButton; // Random object to create random integers private Random generator = new Random(); // two-dimensional array to maintain unique random numbers private boolean[][] uniqueNumber = new boolean[ 4 ][ 40 ]; // one-dimensional array to store strings for output private String[] output = new String[ 4 ]; // instance variable to hold the selected lottery number private int selection; // no-argument constructor public LotteryPicker() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane for attaching GUI components Container contentPane = getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout( null ); // set up oneJLabel oneJLabel = new JLabel(); oneJLabel.setBounds( 16, 18, 100, 16 ); oneJLabel.setText( "First lottery:" ); contentPane.add( oneJLabel ); // set up oneJTextField oneJTextField = new JTextField(); oneJTextField.setBounds( 120, 16, 124, 23 ); oneJTextField.setHorizontalAlignment( JTextField.CENTER ); oneJTextField.setEditable( false ); contentPane.add( oneJTextField ); // set up twoJLabel twoJLabel = new JLabel(); twoJLabel.setBounds( 16, 50, 100, 16 ); twoJLabel.setText( "Second lottery:" ); contentPane.add( twoJLabel ); // set up twoJTextField twoJTextField = new JTextField(); twoJTextField.setBounds( 120, 48, 124, 23 ); twoJTextField.setHorizontalAlignment( JTextField.CENTER ); twoJTextField.setEditable( false ); contentPane.add( twoJTextField ); // set up threeJLabel threeJLabel = new JLabel(); threeJLabel.setBounds( 16, 82, 100, 16 ); threeJLabel.setText( "Third lottery:" ); contentPane.add( threeJLabel ); // set up threeJTextField threeJTextField = new JTextField(); threeJTextField.setBounds( 120, 80, 124, 23 ); threeJTextField.setHorizontalAlignment( JTextField.CENTER ); threeJTextField.setEditable( false ); contentPane.add( threeJTextField ); // set up fourJLabel fourJLabel = new JLabel(); fourJLabel.setBounds( 16, 114, 100, 16 ); fourJLabel.setText( "Fourth lottery:" ); contentPane.add( fourJLabel ); // set up fourJTextField fourJTextField = new JTextField(); fourJTextField.setBounds( 120, 112, 124, 23 ); fourJTextField.setHorizontalAlignment( JTextField.CENTER ); fourJTextField.setEditable( false ); contentPane.add( fourJTextField ); // set up generateJButton generateJButton = new JButton(); generateJButton.setBounds( 148, 150, 96, 24 ); generateJButton.setText( "Generate" ); contentPane.add( generateJButton ); generateJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when generateJButton is clicked public void actionPerformed( ActionEvent event ) { generateJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set properties of application's window setTitle( "Lottery Picker" ); // set title bar string setSize( 264, 212 ); // set window size setVisible( true ); // display window } // end method createUserInterface // generate four random five-number lottery combinations private void generateJButtonActionPerformed( ActionEvent event ) { (HELP HERE) } // end method generateJButtonActionPerformed // generate random number in a given range private int generate( int low, int high ) { // generate random number in range from low to high return low + generator.nextInt( high - low + 1 ); } // end method generate // main method public static void main( String[] args ) { LotteryPicker application = new LotteryPicker(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class LotteryPicker /************************************************************************** * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
There are basically two ways to ensure that no number is repeated. The first way is to randomly permute a list of numbers with no duplicates. Then you can just ask for n numbers from that list and they should be fairly random:
The second way is to build a list straight from the random number generator and check which numbers you've already inserted to avoid inserting it again. There's a large number of ways you can go about this, the simplest being a search in the existing array for each new number:
Java Syntax (Toggle Plain Text)
public class scratch { public static void main(String[] args) { new lottery ( 20 ); } } class lottery { private int[] numbers; public lottery ( int n ) { numbers = new int[n]; // Initialize lottery numbers for ( int i = 0; i < n; i++ ) { numbers[i] = i; } show_all ( numbers ); random_shuffle ( numbers ); show_all ( numbers ); } private void random_shuffle ( int[] list ) { for ( int i = 0; i < list.length - 1; i++ ) { int r = (int)( Math.random() * ( list.length - i ) ); int save = list[i]; list[i] = list[i + r]; list[i + r] = save; } } private void show_all ( int[] list ) { for ( int i = 0; i < list.length; i++ ) { System.out.print ( list[i] + " " ); } System.out.println(); } }
Java Syntax (Toggle Plain Text)
public class scratch { public static void main(String[] args) { new lottery ( 20 ); } } class lottery { private int[] numbers; public lottery ( int n ) { numbers = new int[n]; // Initialize lottery numbers int i = 0; while ( i < n ) { int r = (int)( Math.random() * n ); if ( add ( numbers, i, r ) ) { ++i; } } show_all ( numbers ); } private boolean add ( int[] list, int size, int val ) { for ( int i = 0; i < size; i++ ) { if ( list[i] == val ) { return false; } } list[size] = val; return true; } private void show_all ( int[] list ) { for ( int i = 0; i < list.length; i++ ) { System.out.print ( list[i] + " " ); } System.out.println(); } }
I'm here to prove you wrong.
![]() |
Similar Threads
- Java Programming Help!!!!!!!!!!!!!!!!! (Java)
- Java as a programming platform. (Java)
- Java Programming (Java)
- java programming language (Java)
Other Threads in the Java Forum
- Previous Thread: Keeping a Java App. Open but Closing One of the Windows
- Next Thread: Images
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






