8 Replies - 347 Views - Last Post: 04 March 2011 - 01:02 AM Rate Topic: -----

#1 dag72  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 02-March 11

Trying to compute a potential lottery winner based on the amount of ti

Posted 02 March 2011 - 05:27 PM

Hello there,

I am trying write a program which allows user to input the amount of lottery tickets each player purchases then display a potential winner base on the largest amount of ticket that player purchased. Please see below for what i have done thus far, thus far this allows the user to enter the amount of purchase tickets against each player but i am not getting it to out the potential winner say for example 'lotteryPlayer6' because he/she purchase the most tickets.

Many thanks
import java.util.Scanner;
/**
 This program will allow a user to input the amount of tickets purchase by each player, then the total 
 amount will be calculated and output the amount
 * 
 * 
 * @version (a version number or a date)
 */
public class LotteryPlayers1
{
public static void main(String []args)
    {
    Scanner in = new Scanner(System.in);
    //int lotteryPlayer1 = 0;
    //int lotteryPlayer2 = 0;
    //int lotteryPlayer3 = 0;
    //int lotteryPlayer4 = 0;
    //int lotteryPlayer5 = 0;
    //int lotteryPlayer6 = 0;
    
    System.out.print("Please enter amount of tickets purchase by player1: ");
    int lotteryPlayer1 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player2: ");
    int lotteryPlayer2 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player3: ");
    int lotteryPlayer3 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player4: ");
    int lotteryPlayer4 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player5: ");
    int lotteryPlayer5 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player6: ");
    int lotteryPlayer6 = in. nextInt();   
    //System.out.println(lotteryPlayer1+lotteryPlayer2+lotteryPlayer3+lotteryPlayer4+lotteryPlayer5+lotteryPlayer6);
    System.out.print("Enter the most ticket sales to see the favorite chance: ");
    int lotteryPlayer = in. nextInt();
    
     
   int potentialWinner = 0;
    if (lotteryPlayer >= 100)
    {
        potentialWinner = lotteryPlayer;
    }
    else
   
     {
         potentialWinner = 0;
        }
        System.out.println("the potential winner is: "+potentialWinner+lotteryPlayer);
    }
}


Welcome at DIC. Please :code:

This post has been edited by pbl: 02 March 2011 - 05:29 PM
Reason for edit:: Code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to compute a potential lottery winner based on the amount of ti

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,385
  • Joined: 27-December 08

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 02 March 2011 - 05:36 PM

You should really store an int[], and populate your choices into the array. Then just loop through the array to find the max.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 02 March 2011 - 05:36 PM

Don't really understand your mathematic
All what your program does is:

    System.out.print("Enter the most ticket sales to see the favorite chance: ");
    int lotteryPlayer = in. nextInt();
         
    int potentialWinner = 0;
    if (lotteryPlayer >= 100)
    {
        potentialWinner = lotteryPlayer;
    }
    System.out.println("the potential winner is: "+potentialWinner+lotteryPlayer);
}


So you input a number from the user
if this number is >= 100 you print that number * 2
else you output that number
Was This Post Helpful? 0
  • +
  • -

#4 dag72  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 02-March 11

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 03 March 2011 - 02:52 AM

View Postmacosxnerd101, on 02 March 2011 - 05:36 PM, said:

You should really store an int[], and populate your choices into the array. Then just loop through the array to find the max.


Many thanks, this is very helpful tried that and it is looking better now; still not there yet but its coming. As i am a newbie to this programming thing it will take me a day or two to finally get it working.

View Postpbl, on 02 March 2011 - 05:36 PM, said:

Don't really understand your mathematic
All what your program does is:

    System.out.print("Enter the most ticket sales to see the favorite chance: ");
    int lotteryPlayer = in. nextInt();
         
    int potentialWinner = 0;
    if (lotteryPlayer >= 100)
    {
        potentialWinner = lotteryPlayer;
    }
    System.out.println("the potential winner is: "+potentialWinner+lotteryPlayer);
}


So you input a number from the user
if this number is >= 100 you print that number * 2
else you output that number


Many thanks
Was This Post Helpful? 0
  • +
  • -

#5 dag72  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 02-March 11

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 03 March 2011 - 04:16 PM

Hello there, I have revised this code and get it to print out a string of largest players but the value besides each are all 0 not sure whats going on any suggestions? Please see below.
import java.util.Scanner;
/**
 This program will allow a user to input the amount of tickets purchase by each player, then the total 
 amount will be calculated and output the amount
 * 
 * @author (by Dave Gordon) 
 * @version (a version number or a date)
 */
public class LotteryPlayers1
{
public static void main(String []args)
    {
    Scanner in = new Scanner(System.in);
    final int LENGTH = 10;
    int[] data = new int[LENGTH];
    //int lotteryPlayers = 0;
    //int lotteryPlayer1 = 0;
    //int lotteryPlayer2 = 0;
    //int lotteryPlayer3 = 0;
    //int lotteryPlayer4 = 0;
    //int lotteryPlayer5 = 0;
    //int lotteryPlayer6 = 0;
    
    System.out.print("Please enter amount of tickets purchase by player1: ");
    int lotteryPlayer1 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player2: ");
    int lotteryPlayer2 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player3: ");
    int lotteryPlayer3 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player4: ");
    int lotteryPlayer4 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player5: ");
    int lotteryPlayer5 = in. nextInt();
    System.out.print("Please enter amount of tickets purchase by player6: ");
    int lotteryPlayer6 = in. nextInt();   
    //System.out.println(lotteryPlayer1+lotteryPlayer2+lotteryPlayer3+lotteryPlayer4+lotteryPlayer5+lotteryPlayer6);
    System.out.println("The highest player's chance of winning ");
    //int lotteryPlayer = in. nextInt();
    
    
   int largestPlayer = data[0];
          for (int i = 1; i < largestPlayer; i++)
          {
             if (data[i] > largestPlayer)
              {
                  largestPlayer = data[i];
                  
                }
            }
            for (int i = 0; i < largestPlayer; i++)
            {
                System.out.print(data[i]);
                if (data[i] == largestPlayer)
                {
                    System.out.print(" <== largest player");
                }
                System.out.println();
            }
        }
    }
//}


This post has been edited by macosxnerd101: 03 March 2011 - 05:04 PM
Reason for edit:: Please use code tags!!!

Was This Post Helpful? 0
  • +
  • -

#6 kevin_mchugh  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 66
  • Joined: 08-April 09

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 03 March 2011 - 05:02 PM

Hello,

You appear to have gotten a bit confused. So I will try to help you out.

Firstly,

System.out.print("Please enter amount of tickets purchase by player1: ");
data[0] = in. nextInt();



This will store the first value typed in into your array. You just move onto the next one by incrementing the data element to "data[1]" and so on.

The in order to loop through them all you would use this.


for(int x=0; x<data.lenght();x++){
  
   data[x]...........
}




I would also urge you to read about accumulator variables, this should help you with your problem.

Again, if anyone else disagrees feel free to flame me. Beyond that, well I think I would just be writing the code for you.

However if you have any more questions....just ask :-)

Later

This post has been edited by kevin_mchugh: 03 March 2011 - 05:03 PM

Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 03 March 2011 - 05:13 PM

Where is the data[] array populated beside for data[0] ?
Was This Post Helpful? 0
  • +
  • -

#8 dag72  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 02-March 11

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 04 March 2011 - 12:39 AM

View Postkevin_mchugh, on 03 March 2011 - 05:02 PM, said:

Hello,

You appear to have gotten a bit confused. So I will try to help you out.

Firstly,

System.out.print("Please enter amount of tickets purchase by player1: ");
data[0] = in. nextInt();



This will store the first value typed in into your array. You just move onto the next one by incrementing the data element to "data[1]" and so on.

The in order to loop through them all you would use this.


for(int x=0; x<data.lenght();x++){
  
   data[x]...........
}




I would also urge you to read about accumulator variables, this should help you with your problem.

Again, if anyone else disagrees feel free to flame me. Beyond that, well I think I would just be writing the code for you.

However if you have any more questions....just ask :-)

Later


Hello

Thanks for your help it really makes things clearer, just to clarify are you saying i need to replace each variables int lotteryPlayer? = in.nextInt()with data[?]= in.nextInt();

Regards
Was This Post Helpful? 0
  • +
  • -

#9 dag72  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 02-March 11

Re: Trying to compute a potential lottery winner based on the amount of ti

Posted 04 March 2011 - 01:02 AM

View Postdag72, on 04 March 2011 - 12:39 AM, said:

View Postkevin_mchugh, on 03 March 2011 - 05:02 PM, said:

Hello,

You appear to have gotten a bit confused. So I will try to help you out.

Firstly,

System.out.print("Please enter amount of tickets purchase by player1: ");
data[0] = in. nextInt();



This will store the first value typed in into your array. You just move onto the next one by incrementing the data element to "data[1]" and so on.

The in order to loop through them all you would use this.


for(int x=0; x<data.lenght();x++){
  
   data[x]...........
}




I would also urge you to read about accumulator variables, this should help you with your problem.

Again, if anyone else disagrees feel free to flame me. Beyond that, well I think I would just be writing the code for you.

However if you have any more questions....just ask :-)

Later


Hello

Thanks for your help it really makes things clearer, just to clarify are you saying i need to replace each variables int lotteryPlayer? = in.nextInt()with data[?]= in.nextInt();

Regards

Hello

Thanks again man, you are a genius this has solved my problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1