Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2007, 10:55 AM
Member
 
Join Date: May 2007
Posts: 1
-JaVa- is on a distinguished road
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
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-30-2007, 09:08 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
Try This
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
do...while loop eva New To Java 16 01-31-2008 08:44 AM
while loop michcio New To Java 5 01-27-2008 02:56 AM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 12:20 PM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 10:44 AM
While loop leebee New To Java 1 07-18-2007 05:11 PM


All times are GMT +3. The time now is 08:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org