4 Replies - 2840 Views - Last Post: 10 April 2012 - 06:25 AM Rate Topic: -----

#1 hankhavoc101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-April 12

I need help with my lottery java program, please help

Posted 09 April 2012 - 08:08 PM

So basically I have a lottery program and I need help separating it. I thought this was going to be pretty easy to do but I am having a tough time accomplishing it. My main method is holding too much of the program and I need to create a static method called "public static int [] generateLottoNumbers()" I am having trouble trying to separate the code so my main method can call on generateLottoNumbers so it can provide me with the array of numbers. This is the code that I have that works which is everything in my main method. So I just need advice on how to separate it so I can call generateLottoNumbers() and have it in my main method so then i can display the lottery number.

import java.util.*;

public class Examples
	{

	    public static void main(String[] args)
		{
			int number;
			int pick; 
			 
			
			int chosen[] = new int[5];
			

			int megaNumber = 46; // Range
			int lottoNumber;
			System.out.print("Lottery Numbers: ");
			// Randomly pick seven numbers from 1 to 56
			for (int i = 0; i <=0; i ++)
			{
				//Create a Random Mega Number
				lottoNumber = (int) (megaNumber * Math.random()) + 1;
				
				for (pick = 0; pick < 5; pick ++)
				{	//pick a random number
					number = (int) (56 * Math.random()) + 1;
				
					//check if the number is already been chosen
					for (int x = 0; x < 5; x++)
					{
						while (number == chosen[x])
						{
							//pick a new number if it is already there
							number = (int) (56 * Math.random()) + 1;
						}
					}		
						
					chosen[pick] = number;//Add the number to the array
					
					
					
					System.out.print(chosen[pick]+"  ");
				}		
				System.out.println("\n" + "The Mega Number is " + lottoNumber);
				
			}
	    }
	}


Is This A Good Question/Topic? 0
  • +

Replies To: I need help with my lottery java program, please help

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,075
  • Joined: 02-June 10

Re: I need help with my lottery java program, please help

Posted 09 April 2012 - 10:10 PM

There are more than 500,000 users on the DIC. Can you imagine if they all started threads as "help" ? Please use meaningful titles so the people with the most knowledge in your area are drawn to your question. This also helps people later using search engines to find they help they need.

What exactly do you need help with... or help understanding?
Do you not know what a method is? Do you not understand that they return values?

There doesn't seem to be any attempt to create a new method for generateLottoNumbers().
As this is your homework you need to at least make some effort just so we have some of your code to actually help you with: Otherwise we're just doing it for you.

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

These articles may also apply:
What does this error mean?

Stages of asking for homework help on a forum.
I don't want you to write my code, just give me ideas on how to solve my problem. - Read this article
How NOT to go about your assignment
"It doesn't work" isn't good enough - How you can help us, to help you

What have to you tried to code so far? - Click the link
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: I need help with my lottery java program, please help

Posted 09 April 2012 - 10:15 PM

tlhIn `toq: this is Java. You don't use Visual Studio for Java.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,075
  • Joined: 02-June 10

Re: I need help with my lottery java program, please help

Posted 09 April 2012 - 10:33 PM

yeah. I know. That's why it says IF you are using Visual Studio

The request that the OP actually show us an effort still stands. The suggestion to not delete code attempts but instead comment them out still stands. The articles about how to give us enough information to help still stands.

The only thing that is really any different is the format of the comments in the example. Its a cut/paste example that gets the concept across. No, I did not make comments in C#, VB.NET, Python, XAML and JAVA. Oh wait, the comment delimiter in java is the same as C#: // and /* comment */ So even that is correct here.

I'm not sure what you're getting your knickers in a twist over. G'night
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: I need help with my lottery java program, please help

Posted 10 April 2012 - 06:25 AM

The simplest way to generate non repeating numbers is to fill an ArrayList with all possible numbers and then, removing randomly from it

http://www.dreaminco...snippet2992.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1