Results 1 to 4 of 4
Thread: How to use do.. while loop
- 06-30-2007, 08:55 AM #1
Member
- Join Date
- May 2007
- Posts
- 1
- Rep Power
- 0
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
How do we do the above question? Thank you.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); } }
- 06-30-2007, 07:08 PM #2
Member
- Join Date
- Jun 2007
- Posts
- 11
- Rep Power
- 0
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); } }
- 08-30-2010, 07:03 PM #3
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.
-
I would worry more about answering current questions and not questions over 3 years old.
Similar Threads
-
do...while loop
By eva in forum New To JavaReplies: 16Last Post: 01-31-2008, 06:44 AM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM -
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07-18-2007, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks