-3

How can i make a loop that ask the user if they would like to buy more tickets and how can i make the random number generator make unique numbers. if you can help or show me website to find out how can i do this that will be great

Thank you

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

public class PowerBallm {
  //declaring the main method
  public static void main(String[] args) 
  {
    //prompt user to enter amount of ticket to buy between 1 and 5
    System.out.print("How many tickets would you like to purchase ? ");  
    Scanner amount = new Scanner (System.in);
    double ticket_amount;
    ticket_amount = amount.nextInt();
    while (ticket_amount > 5 || ticket_amount <= 0)
    {
      System.out.println("You can only purchase up to FIVE tickets, please try again");
      while (!amount.hasNextInt())
      {
        amount.next();
      }
      ticket_amount = amount.nextInt();
    }
    //creating an Array of 5 numbers
    int group1[] = new int[5];

    for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++)
    {
      for(int i = 0; i < group1.length; i++)
      {
        group1[i] = 1 + (int) (Math.random() * 56);
      }     

      //sort the elements
      //Arrays.sort(group1);

      //group2 number 
      int group2 = 1 + (int) (Math.random()*46);

      //print the ticket numbers numbers
      System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) + " your group2 ticket number is " + group2);      
    }
  }
}
  • What is the program intended to do? – Christian Tapia Jan 18 '14 at 01:29
  • to create a powerball program that ask the user for the number of ticket they would like to purchase and then it will ramdonly generat 5 different numbers for the tickets @Christian – user3208537 Jan 18 '14 at 01:39
  • You really need to focus on formatting your code correctly. This is very hard to read and will deter many users from even looking at it, let alone actually working on it. – takendarkk Jan 18 '14 at 01:43
  • what do you mean @csmckelvey – user3208537 Jan 18 '14 at 01:47
  • Your indents and spacing are not consistent, you have large sections of whitespace for no apparent reason, we don't need to see comments that are self explanatory(like //creating int[]), and you have additional brackets where they aren't needed. I will edit it for you to see. – takendarkk Jan 18 '14 at 01:56
  • how do i create a loop to ask the user if he want to buy more tickets or not.. and how can i make the random generate unique numbers Thank you @davidkonrad – user3208537 Jan 18 '14 at 22:35

2 Answers2

1

Apart from checking for an invalid number of tickets, your code does nothing with the entered number. You need to put the code to create and print a ticket in a for loop.

lincb
  • 622
  • 5
  • 20
1

You could wrap the code from where you're generating the contents of the ticket up to where you display those numbers in another loop like so.

for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++)
{
    for(int i = 0; i < group1.length; i++)
    {
        group1[i] = 1 + (int) (Math.random() * 56);
    }    

    //sort the elements
    Arrays.sort(group1);

    //group2 number 
    int group2 = 1 + (int) (Math.random()*46);

    //print the ticket numbers numbers
    System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) 
        + " your group2 ticket number is " + group2);
}
PakkuDon
  • 1,627
  • 4
  • 22
  • 21