Results 1 to 4 of 4
  1. #1
    -JaVa- is offline Member
    Join Date
    May 2007
    Posts
    1
    Rep Power
    0

    Post How to use do.. while loop

    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
    Java Code:
    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.

  2. #2
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default

    Try This
    Java Code:
    import java.util.Scanner;
    public class GetNewTicketPrice{
    	public static void main(String [] args){
    		
    		int age, ticketPrice=0, day;
    		Scanner sc = new Scanner(System.in);
    		
    		
     do
    {
    
    	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{
    			System.out.print("You have entered an invalid number.");
    		}
    
    } while(age<0 || age>130);
    
    		System.out.print("Your ticket price is: " + ticketPrice);
    	}
    }

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    If you're going to provide a working code for someone who asks a question, please add comments to it and explain how it works and why you did what you did. Otherwise, the OP learns nothing except that he will get free coding done if he comes here.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  2. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  3. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •