Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 15th, 2009, 11:18 PM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
Smile Lotto program

I'm working on a lotto program and i'm currently having trouble with one spot, at least right now. When i print out tickets with their numbers; the numbers printed correspond to the number of tickets that i enter to print. For example: If I enter: 2 for the # of tickets then i will get two tickets but only the first two numbers of six have a value greater that one. I can't seem to figure out why? Any help would be greatly appreciated!

Reply With Quote
  #2  
Old November 16th, 2009, 01:14 AM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
Any chance you could post your code?

Reply With Quote
  #3  
Old November 18th, 2009, 08:53 PM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
i dont know what's wrong the lotto # generator won't work today.last night kinda

import java.util.*;

public class MegaLottery {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

//Find the nuber of tickets to be generated
System.out.println("Please enter the number of tickets that you " +
"would like to be generated.");
int n = s.nextInt();

System.out.print("The number of tickets you selected is " );


//call method
generateLottoTickets(n);




//create method to generate lotto tickets
}
public static int[][] generateLottoTickets(int n) {

int[][] ticketArray = new int[n][5];
for (int row = 0; row < ticketArray.length; row++) {
for (int column = 0; column < ticketArray.length; column++) {
ticketArray[row][column] = (int)(Math.random() * 49);

}
}
//generate random lottery numbers
int[] ticketNums = new int[49];
for (int i = 0; i < ticketNums.length ; i++) {
ticketNums[i] = (int) (Math.random() * 49);

}

//print the tickets in the array
for (int row = 0; row < ticketArray.length ; row++) {
for (int column = 0; column < ticketArray[row].length +2; column++) {
System.out.print(ticketArray[row][column] + " ");
}
System.out.println();
}

return ticketArray;
}


}

Reply With Quote
  #4  
Old November 18th, 2009, 08:58 PM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
Each time i try to fix it gets worse. the ticket generator worked a little last nigh

Quote:
Originally Posted by paulscode
Any chance you could post your code?

Reply With Quote
  #5  
Old November 18th, 2009, 09:32 PM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
First thing I noticed, is that the formatting is haphazard. There may be improperly matched brackets. If I were you, I would get into a habit of aligning code by indenting between brackets.

Second thing I noticed is that you forgot to print the number of tickets they entered (a minor error)

Third thing I noticed is that you are generating the random tickets in two places. The second place (the 1d array) is not needed.

Fourth thing I noticed (also the main source of your problems) is the upper limits of your nested for loops are wrong. You have each of them counting to the same number, but there are n rows and 5 columns in the array, so those are the values you should use for the upper limits of your for loops.

Corrected, your program should look something like this (I haven't actually tested it):
Code:
import java.util.*;

public class MegaLottery
{
    public static void main( String[] args )
    {
        Scanner s = new Scanner( System.in );
        //Find the nuber of tickets to be generated
        System.out.println( "Please enter the number of tickets that you would like to be generated.");
        int n = s.nextInt();
        System.out.print( "The number of tickets you selected is " + n );

        //call method
        generateLottoTickets( n );
        //create method to generate lotto tickets
    }
    public static int[][] generateLottoTickets( int n )
    {
        int[][] ticketArray = new int[n][5];
        //generate random lottery numbers
        for( int row = 0; row < n; row++ )
        {
            for( int column = 0; column < 5; column++ )
            {
                ticketArray[row][column] = (int)(Math.random() * 49);
            }
        }
        //print the tickets in the array
        for( int row = 0; row < n ; row++ )
        {
            for( int column = 0; column < 5; column++ )
            {
                System.out.print( ticketArray[row][column] + " " );
            }
            System.out.println();
        }
        return ticketArray;
    }
}


The only other thing I would suggest changing is moving the part that prints out the contents of the 2d array into a seperate method. It wouldn't change the behavior of your program, but it would make it look a little cleaner.

Let me know if you need any further explanation or if you have a problem getting the program to work.

Last edited by paulscode : November 18th, 2009 at 09:41 PM.

Reply With Quote
  #6  
Old November 19th, 2009, 10:53 AM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
Smile Thanks

Quote:
Originally Posted by paulscode
First thing I noticed, is that the formatting is haphazard. There may be improperly matched brackets. If I were you, I would get into a habit of aligning code by indenting between brackets.

Second thing I noticed is that you forgot to print the number of tickets they entered (a minor error)

Third thing I noticed is that you are generating the random tickets in two places. The second place (the 1d array) is not needed.

Fourth thing I noticed (also the main source of your problems) is the upper limits of your nested for loops are wrong. You have each of them counting to the same number, but there are n rows and 5 columns in the array, so those are the values you should use for the upper limits of your for loops.

Corrected, your program should look something like this (I haven't actually tested it):
Code:
import java.util.*;

public class MegaLottery
{
    public static void main( String[] args )
    {
        Scanner s = new Scanner( System.in );
        //Find the nuber of tickets to be generated
        System.out.println( "Please enter the number of tickets that you would like to be generated.");
        int n = s.nextInt();
        System.out.print( "The number of tickets you selected is " + n );

        //call method
        generateLottoTickets( n );
        //create method to generate lotto tickets
    }
    public static int[][] generateLottoTickets( int n )
    {
        int[][] ticketArray = new int[n][5];
        //generate random lottery numbers
        for( int row = 0; row < n; row++ )
        {
            for( int column = 0; column < 5; column++ )
            {
                ticketArray[row][column] = (int)(Math.random() * 49);
            }
        }
        //print the tickets in the array
        for( int row = 0; row < n ; row++ )
        {
            for( int column = 0; column < 5; column++ )
            {
                System.out.print( ticketArray[row][column] + " " );
            }
            System.out.println();
        }
        return ticketArray;
    }
}


The only other thing I would suggest changing is moving the part that prints out the contents of the 2d array into a seperate method. It wouldn't change the behavior of your program, but it would make it look a little cleaner.

Let me know if you need any further explanation or if you have a problem getting the program to work.



I planned on implementing the other into another method by my problem was that the user can enter no more than six tickets. I don't know why. I''l try the changes you gave me. Thanks so much!!

Reply With Quote
  #7  
Old November 19th, 2009, 11:47 PM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
Exclamation

I have one other problem! The final method i'm to used is :
public static boolean isCoveringAllBases(int[][] tickets)

it is supposed to check whether the tickets passed as a 2-d array, covers all the bases.
I have no idea how to use this because i don't know how to pass variables into this method. I'm at a total loss!!!!! PLEASE HELP. This is due tomorrow and i'm at a loss.
Thanks, Daniel

Reply With Quote
  #8  
Old November 20th, 2009, 07:25 AM
paulscode's Avatar
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 86 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 18 m 37 sec
Reputation Power: 11
I have no idea what "covering all the bases" means, however it is pretty simple to call a method. First you have to create the method:

Code:
public static boolean isCoveringAllBases(int[][] tickets, int n)
{
    // TODO:  Do something here..
}


Then, in your code, you can call the method by passing it the array that gets returned by your generateLottoTickets method. To do that, first find the part of your code that looks like this:
Code:
        //call method
        generateLottoTickets( n );
        //create method to generate lotto tickets


Change it to something like this:

Code:
        //call method
        int[][] lottoTickets = generateLottoTickets( n );
        if( isCoveringAllBases( lottoTickets, n ) )
            System.out.println( "All bases are covered!" );
        else
            System.out.println( "Not all bases are covered!" );


Hope this helps.

Reply With Quote
  #9  
Old November 20th, 2009, 11:37 PM
nemolilo nemolilo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Georgia
Posts: 6 nemolilo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 5 sec
Reputation Power: 0
Thumbs up Freds Lotto Program (From ACM programming contest)

Thanks I finally figured it out. What i meant by covering all the bases is that the user wants to have enough tickets so that each set will contain every number 1-49 at least once, on some ticket. Here is the code that i finally figured out and it works(thanks soooo much): Here is the project from my professor:
/* Author,
* Daniel Perry*/





import java.util.*;




public class megaloterycorrect {


public static void main(String args[]){
Scanner s = new Scanner(System.in);

//Ask how many tickets you want made (49/6 = 8, so it cant be less than 9)
System.out.print("please enter the number of tickets you want.\nYou must " +
"enter more than 8 for a chance to cover all the numbers from 1-49 " +
"at least once on some ticket.\n");
int n = s.nextInt();

//check and see if the person actually put in a number less than 9
if ( n <= 8)
System.out.print("That amount of tickets won't cover all numbers 1-49");

else{
System.out.println("These tickets may cover all the bases of numbers 1-49");
int[][] ticket = generateLottoTicket(n);

//check for all of the numbers from 1-49 to appear at least once on some ticket
coverAllBases(ticket);

//if false,ask if they want to try some again

while (coverAllBases(ticket) == false){
System.out.print("Thanks. Here are your tickets: \n");
for(int i = 0; i < ticket.length; i++){
for(int j = 0; j < ticket[i].length; j++){
System.out.print(ticket [i][j] + " ");
}
System.out.println();
}
System.out.print("Sorry this doesn't have all of the numbers from 1-49.\n" +
" Would you like to try again (1 = yes, 0 = no)?\n");
int ans = s.nextInt();
if (ans == 1){
ticket = generateLottoTicket(n);
coverAllBases(ticket);
}
else if (ans == 0){
System.out.print("Thanks for playing, sorry the tickets didn't cover all the bases.\n");
break;
}
}

//what do you do when it is true
if (coverAllBases(ticket) == true){
System.out.print("Thanks. Your numbers are:\n");
for(int i = 0; i < ticket.length; i++){
for(int j = 0; j < ticket[i].length; j++){
System.out.print(ticket [i][j] + " ");
}
System.out.println();
}
System.out.print("All of the numbers from 1-49 are included \n");
}

}
}

public static int[][] generateLottoTicket(int n){
int[][] ticket = new int[n][6];
for(int i = 0; i < ticket.length; i++){
int c = 0;
for(int j = 0; j < ticket[i].length; j++){
ticket[i][j] = (int)(Math.random() * 49 + 1);
c = ticket [i][j];
}
for(int k = 0; k < ticket[i].length; k++){
if (c == ticket [i][k]){
ticket [i][k] = (int)(Math.random() * 49 + 1);
c = ticket [i][k];
}
}

}
return ticket;
}

public static boolean coverAllBases(int[][]b){
int[] a = new int[49];
int num = 1;
while(num <= 49){
for(int j = 0; j < b.length; j++){
for(int k = 0; k < b[j].length; k++){
if (b[j][k] == num)
a [num - 1] ++;
}
}
num++;
}
for(int l = 0; l < a.length; l++){
if (a[l] == 0){
return false;
}
}
return true;
}
}

Last edited by nemolilo : November 20th, 2009 at 11:45 PM. Reason: Added the solution and explanation

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Lotto program


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap