generate six numbers from 1 to 76 with no repeats 1 prompt u

generate six numbers from 1 to 76 with no repeats
1) prompt user for amount tickets they want to purchase must be between 1 and 7
2) generate six random numbers (1-76) for each ticket
3) display ticket one per line with numbers in ascending order
ex 4 12 45 61 68 70
2 9 16 23 50 51 74
must use a set data structure built with a treeset constructor
must prompt user for number of tickets if number out of range then reprompt until a valid number (1-7)

Solution

Please follow the code and comments for description :

CODE :

import java.util.Random; // required imports for the code
import java.util.Scanner;
import java.util.TreeSet;

public class randomTicketGen { // class to get the code run
  
public static void main(String[] args) { // driver method
  
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
TreeSet<Integer> values = new TreeSet<>(); // tree set to save the data
StringBuffer sb = new StringBuffer(); // buffer value to print the data
  
System.out.println(\"Please Enter the Number of Tickets You Want (Must be in Between 1 and 7) : \"); // prompt for the user to enter the data
int tickets = sc.nextInt();// get the data
while (tickets > 7 || tickets <= 0) { // check for the user input
System.out.println(\"Number is Invalid/Out of Range.\ Enter Again : \"); // message for invalid data
tickets = sc.nextInt(); // get again the data
}
  
for (int i = 0; i < tickets; i++) { // iterating over the number of tickets
int Low = 1; // required initialisations
int High = 77;
  
while (values.size() < 6) { // generating 6 random numbers
Random r = new Random(); // class that generates the random numbers
int Result = r.nextInt(High - Low) + Low; // setting the limits
values.add(Result); // adding the data to the set
}
  
for (int res : values) { // iterating over the set to get the data
sb.append(res).append(\" \"); // append the data tot he buffer
}
           System.out.println(\"The Tickets are : \");
System.out.println(\"Ticket \" + (i + 1) + \" : \" + sb.toString()); // print the data to console
sb.setLength(0); // empty the buffer
values.clear(); // empty the set
}
}
}


OUTPUT :

Please Enter the Number of Tickets You Want (Must be in Between 1 and 7) :
8
Number is Invalid/Out of Range.
Enter Again :
0
Number is Invalid/Out of Range.
Enter Again :
7
The Tickets are :
Ticket 1 : 8 10 16 28 55 60
Ticket 2 : 14 24 36 44 64 65
Ticket 3 : 25 27 36 37 61 73
Ticket 4 : 11 12 45 53 70 75
Ticket 5 : 40 43 47 56 65 68
Ticket 6 : 42 50 54 57 68 72
Ticket 7 : 3 16 19 60 71 74


Hope this is helpful.

generate six numbers from 1 to 76 with no repeats 1) prompt user for amount tickets they want to purchase must be between 1 and 7 2) generate six random numbers
generate six numbers from 1 to 76 with no repeats 1) prompt user for amount tickets they want to purchase must be between 1 and 7 2) generate six random numbers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site