troubleshooting Question

Using Java how do I use a class type array to store user details

Avatar of aspnet-scotland
aspnet-scotlandFlag for United Kingdom of Great Britain and Northern Ireland asked on
Java
5 Comments1 Solution473 ViewsLast Modified:
Hi,

I have the attached code.

I have purposly only used one class and arrays as this was a requirement. How can I modify the code to allow the user to place up to 20 bets (each bet is 6 different numbers between 1 and 49) at a cost of £1 per bet. Basically I need to modify my class to also prompt the user for his/her name and how many bets they would like to make (up to 20 bets) keeping track of how much they are due for the amount of bets they have made. I also need to give the user the option of a lucky dip where the program chooses 6 random numbers for them.

import java.util.Scanner;
import java.util.Random;

public class LotteryNumbers
{
 public static void main(String[] args)
 {
     System.out.println( "Play Lottery : 6/49" + "\n");

  // Part 1: Pick six  numbers, between 1 and 49.
  // and put them in an arry of six numbers

  int [] P = new int [6];

  // Create a Scanner input from keyboard
      System.out.println("Part1:  \n");
  System.out.println( "£1 for a line of six numbers. Play!" + "\n");

  System.out.println("Enter your lottery numbers- enter one number at a time.");

   for (int index =0; index <6; index++)
    {
     Scanner keyboard = new Scanner (System.in);
     System.out.print("# " + (index+1) + ": ");
     P[index] = keyboard.nextInt();
       }
         while (
        (P[0] < 1|| P[0] >49)||
              (P[1] < 1|| P[1] >49)||
              (P[2] < 1|| P[2] >49)||
              (P[3] < 1|| P[3] >49)||
              (P[4] < 1|| P[4] >49)||
              (P[5] < 1|| P[5] >49)

              ||

              P[0] == P[1] || P[0] == P[2] || P[0] ==P[3] || P[0]== P[4] || P[0] ==P[5]||

              P[1] == P[2] || P[1] ==P[3] || P[1] ==P[4] || P[1]== P[5] ||

              P[2] == P[3] || P[2] ==P[4] || P[2] ==P[5] ||

              P[3] == P[4] || P[3] ==P[5] ||
              P[4] == P[5]
              )

              {
                  System.out.println( "Error: Please use numbers between 1 and 49 inclusive, and no repititions");

       for (int index =0; index <6; index++)
         {

        Scanner keyboard = new Scanner (System.in);
        System.out.print(" Pick # " + (index+1) + ": ");
           P[index] = keyboard.nextInt();

         }

          }


       // Part 1B": Put your   numbers pick in inmcreasing order. Sort the numbers from lowest to highest.

       int startScan, index, minIndex, minValue;

       for ( startScan= 0; startScan <(P.length-1); startScan++)

       {
     minIndex = startScan;
     minValue = P[startScan];

     for ( index = startScan +1; index <P.length; index++)
     {
      if (P[index] < minValue)
      {
       minValue = P[index];
       minIndex =index;
      }

     }
      P[minIndex] = P[startScan];
      P[startScan] =minValue;

     }

  System.out. println ( "This is the sorted list of your  numbers:");

  for (int j =0; j <P.length; j++)

   System.out. print (  P[j] + "  ");

  System.out.println ("End of part 1 \n\n");
        System.out.println();

       // Part 2: Generate 6 random numbers for lottery draw
        System.out.println( "Part2:");

       // Create a Random class object.


        // Let the lottery numbers be:

  int N1, N2, N3, N4, N5, N6 ;   // random numbers

  Random randomNumber = new Random();

        // Let

        N1=1;  N2 = 1;  N3 = 1; N4 = 1; N5=1;  N6= 1;


        while ( N1 <1 ||  N2 <1 || N3 <1 || N4 <1 ||N5 < 1 ||  N6 <1 ||

                 N1==N2 || N1==N3 || N1==N4 || N1==N5 || N1==N6||
                 N2==N3 || N2==N4 || N2==N5 || N2==N6 ||
                 N3==N4 || N3==N5 || N3==N6 ||
                 N4==N5 || N4==N6 ||
                 N5==N6
               )
        {

  N1 = randomNumber.nextInt(49);   N2 = randomNumber.nextInt(49);       N3 = randomNumber.nextInt(49);

        N4 = randomNumber.nextInt(49);     N5 = randomNumber.nextInt(49);        N6 = randomNumber.nextInt(49);

     }

       // Create an array of the Lottery random numbers

       int [] lotteryNumbers  = new int [6];

        lotteryNumbers[0] = N1;    lotteryNumbers[1] = N2;      lotteryNumbers[2] = N3;
        lotteryNumbers[3] = N4;    lotteryNumbers[4] = N5;      lotteryNumbers[5] = N6;

       System.out. println ("The Random-Draw Lottery  Numbers are:");
           for (int j =0; j <lotteryNumbers.length; j++)

           System.out. println ( "#  " + (j+1) + "\t"  + lotteryNumbers[j]);


           System.out. println ();

        // sort the numbers from lowest to highest.

       for ( startScan= 0; startScan <(lotteryNumbers.length-1); startScan++)

       {
     minIndex = startScan;
     minValue = lotteryNumbers[startScan];

     for ( index = startScan +1; index <lotteryNumbers.length; index++)
     {
      if (lotteryNumbers[index] < minValue)
      {
       minValue = lotteryNumbers[index];
       minIndex =index;
      }

     }
      lotteryNumbers[minIndex] = lotteryNumbers[startScan];
      lotteryNumbers[startScan] =minValue;

     }
          System.out. println( "This is the  sorted list of lottery-draw numbers:");

          for (int j =0; j <lotteryNumbers.length; j++)

           System.out. print ( lotteryNumbers[j] + "  ");

           //

        System.out.println( "End of part 2 \n\n");

        System.out.println();

          // Part 3: check lottery numbers - see if P[] == lotteryNumbers[]
          // Let

         System.out.println( "Part3: lottery check!"+ "\n\n");

         int count = 0;

         for ( index = 0; index <6;index++)

          {
     for (int L = 0; L <6; L++)

           if ( P[index] == lotteryNumbers[L])

            count++;
      }

         System.out.println("You matched:  " + count + " number(s)!");

          if( count == 0 )
          System.out.println( "Sorry, You are not a winner this time."+ "\n" );

          if( count == 1 )
          System.out.println( "You  win £1."+"\n" );

          if( count == 2 )
          System.out.println( "You win £2."+"\n");


          if( count == 3)

          System.out.println( "You win £3."+"\n");

          if( count == 4)

          System.out.println( "You win £4."+"\n");

          if( count == 5)

          System.out.println( "You win £50."+"\n");

          if( count == 6)

          System.out.println( "You  win the jackpot of £1000."+"\n");

 }
}

Thanks.
ASKER CERTIFIED SOLUTION
gatorvip

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros