In: Computer Science
A radio station is selling concert tickets containing a random number from 1-100. Write JAVA program that prompts the user for the number of concert tickets they’d like to purchase. Use a while loop to output each ticket followed by a randomly generated number from 1-100. For example, if the user purchases 3 concert tickets, the output could look like this:
Ticket 1: 79
Ticket 2: 19
Ticket 3: 61
Java code:
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
//creating a Random
object
Random rand=new
Random();
//asking for number of
concert tickets
System.out.print("Enter the number
of concert tickets they’d like to purchase: ");
//accepting it
int number=input.nextInt();
//initializing i as 1
int i=1;
//looping till number
while(i<=number){
//printing
the ticket
System.out.println("Ticket "+i+": "+(rand.nextInt(100)+1));
//incrementing i
i++;
}
}
}
Screenshot:
Input and Output: