Hi, I have a question on do.. while loop. Below is the question.
Create a new Java program LoopTicketPrice.java. Copy the content of GetNewTicketPrice.java from the previous practical to LoopTicketPrice.java. Modify the program such that if the age keyed in by the user is invalid, appropriate error messages are displayed before asking the user to re-enter again. Use do.. while loop.
Below is the coding from GetNewTicketPrice.java
import java.util.Scanner;
public class GetNewTicketPrice{
public static void main(String [] args){
int age, ticketPrice, day;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
age = sc.nextInt();
if (age >=4 && age <16){
System.out.print("Enter the day of the week: ");
day = sc.nextInt();
if (day == 6 || day == 7){
ticketPrice = 10;
}
else{
ticketPrice = 7;
}
}
else if (age >=16 && age <65){
System.out.print("Enter the day of the week: ");
day = sc.nextInt();
if (day == 6 || day == 7){
ticketPrice = 10;
}
else{
ticketPrice = 10;
}
}
else if (age >=65 && age <=130){
System.out.print("Enter the day of the week: ");
day = sc.nextInt();
if (day == 6 || day == 7){
ticketPrice = 10;
}
else{
ticketPrice = 5;
}
}
else{
ticketPrice = 0;
System.out.print("You have entered an invalid number.");
System.exit(0);
}
System.out.print("Your ticket price is: " + ticketPrice);
}
}
How do we do the above question? Thank you.