Loop (Unconditional)

icon picker
workshop tickets 2

create a personalised ticket for each person in a group

requirement

The program should allow the user to enter the name of a workshop event at a conference and the number of tickets required for a group
the program should allow the user to enter the names of each person in the group then create a personalised ticket for each person
each ticket should include the name of the workshop, the name of the group member and the number of the ticket

expected output

example output: 5 tickets
Which workshop are you going to
passwords
How many tickets:
2
Enter student name
Charlie
- - - - - - - - - - - - - - -
Interrupt19 - Cyber Conference
Workshop: passwords
Student Admission: Charlie
ticket number: 1
- - - - - - - - - - - - - - -

Enter student name
Dan
- - - - - - - - - - - - - - -
Interrupt19 - Cyber Conference
Workshop: passwords
Student Admission: Dan
ticket number: 2
- - - - - - - - - - - - - - -

design

design : a line of code for one ticket nested within a loop
enter workshop name
enter number of tickets
repeat for number of tickets
enter name
display personalised and numbered ticket for workshop
end repeat

learning-point

loop code
a for-loop in java will repeat the code nested within the loop a fixed number of times (cycles)
the number of cycles can be entered by the user
the value of the loop control variable can be used to display a sequence number by adding 1

introducing the code

1 System.out.println("How many tickets: ");
2 tickets = Integer.parseInt(keyboard.nextLine());

3 for (int i = 0; i < tickets; i++) {
4 System.out.println("Enter student name");
5 student = keyboard.nextLine();

6 System.out.println("- - - - - - - - - - - - - - -");
7 System.out.println(" Interrupt19 - Cyber Conference");
8 System.out.println(" Workshop: " + workshop);
9 System.out.println(" Student Admission: " + student);
10 System.out.println(" ticket number: " + (i + 1));
11 System.out.println("- - - - - - - - - - - - - - -");
12 System.out.println();
13 }
line 1, 2 allows the user to enter the number of tickets and stores it in the variable tickets
line 3 sets up the loop control variable and uses the tickets variable to limit the cycles
lines 4-12 create 1 ticket
line 9 adds 1 to the loop control variable to display the sequence 1, 2, 3 .. rather than 0, 1, 2

developer task

Run the example code in the program, enter test values for the workshop and look at the output.
Read the code. Try to recognise the names, types and values for each variable.
Identify the for-loop control structure.
Notice the loop control code in parenthesis ( ).
Notice the loop control variable: i,
Notice the nested instructions within the control structure in braces { }.
Compare the code with the output. Check out how the variables are used to display the ticket.
Notice the ticket number values now improved to: 1, 2, 3 .. (rather than : 0, 1, 2 .. )
This example will help you in the next exercise, so study it carefully!
read the comment lines in the program to develop similar code for the additional requirement described above
choose suitable test values to ensure that the new code works correctly

test values

test value 1: cryptography (workshop), 2 (tickets), Charlie and Dan (names)

the program code


Loading…
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.