Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: How to make program ask for options again

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default How to make program ask for options again

    I'm trying to make this program to ask for this:
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
    whenever either "o" 's is done

    /* 
     This is a program for a small shop called Pies, Pies and Pis.
     The shop sells pizza pies, cherry pies and gold pi charms. */
     
    import java.util.Scanner; 
    public class asgmt {
     
    public static void main (String [] args) {
    int x , y, o, p, pp, cps, pico, co; 
     
    Scanner inScan = new Scanner(System.in);
     
    System.out.println("Welcome to Pies, Pies and Pis");
    System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
    x = inScan.nextInt();
    if(x == 1){
        System.out.println("Are you a Pie card member? (1 = yes, 2 = no)");
        y = inScan.nextInt();
     
    if(y == 1){
     
    System.out.println("Welcome Back, Pie Card holder!:\n" +
    "You will receive:\n" + "        Pepperoni pizza for the price of plain\n" +
    "        $0.25 off a slice of cherry pie\n" +
    "        $2.00 off a whole cherry pie\n" + "        $10.00% off each Pi charm"); 
    }
     
     
     
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt(); 
     
     
    if(o == 1){
        System.out.println("Here is your current order:\n" +
    "        No pizzas ordered");
    System.out.println("How many plain pizzas would you like for $10.00 each?");
    p = inScan.nextInt();
    System.out.println("How many pepperoni pizzas would you like for $12.00 each?");
    pp = inScan.nextInt();
    }
     
    if(o == 2){
     
    System.out.println("Here is your current order:\n" +
    "        No cherry pie slices ordered");
    System.out.println("How many cherry pie slices would you like for:\n" +
    "        $2.00 per slice\n" +"        $10.00 per pie (6 slices)");
    cps = inScan.nextInt();
    }
     
     
     
    if(o == 3){
     
    System.out.println("Here is your current order:\n" +
    "No gold Pi charm ordered");
    System.out.println("How many Pi charms would you like for $50.00 each?");
    pico = inScan.nextInt();
    }
    if(o == 4){
    System.out.println("Here is your subtotal:\n\n" +
    "       No items purchased! Thanks anyway for stopping!");
    co = inScan.nextInt();
                        }
     
     
        }
                }    
            }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to make program ask for options again

    I don't understand, what is your question? Please be specific, for example, if you have problems with "this part" of the code, then explain what it is doing and what you want it to do. If there are any error messages post those also.

  3. The Following User Says Thank You to jps For This Useful Post:

    m7abraham (September 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: How to make program ask for options again

    Take a look at this tutorial on switch statements:
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    It could do what you ask, and you can use the default to catch any invalid entries, and also loop back to the beginning after they do whatever they want in their respective "o". Atleast I believe that is what you are trying to do, please correct me if I'm wrong.

  5. The Following User Says Thank You to ChicoTheMan94 For This Useful Post:

    m7abraham (September 25th, 2012)

  6. #4
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

    What im trying to do is to let the program prompt this message:
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
    after
    if(o == 1)
    if(o == 2)
    if(o == 3)
    if(o == 4)
    is it more clear now?
    It's kinda hard to explain

    this is how the output is suppose to look like

    Welcome to Pies, Pies and Pis
    Is there a customer in line? (1 = yes, 2 = no) > 1
    Are you a Pie Card member? (1 = yes, 2 = no) > 2
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    6
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    -1
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    1
    Here is your current order:
    	No pizzas ordered
    How many plain pizzas would you like for $10.00 each?
    2
    How many pepperoni pizzas would you like for $12.00 each?
    3
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    4
    Here is your subtotal:
     
    	2 plain pizzas at $10.00ea.:			$20.00
    	3 pepperoni pizzas at $12.00ea.:		$36.00
    							-------
    	Subtotal:					$56.00
    	Tax:						$3.92
    							-------
    	Total:						$59.92
     
     
    	Please enter your payment amount:		$50
     
    	Please enter your payment amount:		$55
     
    	Please enter your payment amount:		$60
    	Your change:					$0.08
    	Thank you for shopping at PP&P!
     
    Welcome to Pies, Pies and Pis
    Is there a customer in line? (1 = yes, 2 = no) > 1
    Are you a Pie Card member? (1 = yes, 2 = no) > 1
    Welcome Back, Pie Card holder!
    You will receive: 
    	Pepperoni pizza for the price of plain
    	$0.25 off a slice of cherry pie
    	$2.00 off a whole cherry pie
    	$10.00% off each Pi charm 
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    2
    Here is your current order:
    No cherry pie slices ordered
    How many cherry pie slices would you like for:
    	$1.75 per slice
    	$8.00 per pie (6 slices)
    15
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    3
    Here is your current order:
    No gold Pi charms ordered
    How many Pi charms would you like for $45.00 each?
    2
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    2
    Here is your current order:
    	2 whole cherry pies
    	3 cherry pie slices
    How many cherry pie slices would you like for:
    	$1.75 per slice
    	$8.00 per pie (6 slices)
    20
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    4
    Here is your subtotal:
     
    	3 whole cherry pies at $8.00ea.:		$24.00
    	2 cherry pie slices at $1.75ea.:		$3.50
    	2 14K gold Pi charms at $45.00ea.:		$90.00
    							-------
    	Subtotal:					$117.50
    	Bonus discount of 10%:				($11.75)
    							-------
    	New subtotal:					$105.75
    	Tax:						$7.40
    							-------
    	Total:						$113.15
     
     
    	Please enter your payment amount:		$120
    	Your change:					$6.85
    	Thank you for shopping at PP&P!
     
    Welcome to Pies, Pies and Pis
    Is there a customer in line? (1 = yes, 2 = no) > 1
    Are you a Pie Card member? (1 = yes, 2 = no) > 3
    Are you a Pie Card member? (1 = yes, 2 = no) > 2
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    1
    Here is your current order:
    	No pizzas ordered
    How many plain pizzas would you like for $10.00 each?
    5
    How many pepperoni pizzas would you like for $12.00 each?
    -3
    Negative number taken as 0
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    2
    Here is your current order:
    No cherry pie slices ordered
    How many cherry pie slices would you like for:
    	$2.00 per slice
    	$10.00 per pie (6 slices)
    7
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    3
    Here is your current order:
    No gold Pi charms ordered
    How many Pi charms would you like for $50.00 each?
    1
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    4
    Here is your subtotal:
     
    	5 plain pizzas at $10.00ea.:			$50.00
    	1 whole cherry pies at $10.00ea.:		$10.00
    	1 cherry pie slices at $2.00ea.:		$2.00
    	1 14K gold Pi charms at $50.00ea.:		$50.00
    							-------
    	Subtotal:					$112.00
    	Tax:						$7.84
    							-------
    	Total:						$119.84
     
     
    	Please enter your payment amount:		$120
    	Your change:					$0.16
    	Thank you for shopping at PP&P!
     
    Welcome to Pies, Pies and Pis
    Is there a customer in line? (1 = yes, 2 = no) > 1
    Are you a Pie Card member? (1 = yes, 2 = no) > 1
    Welcome Back, Pie Card holder!
    You will receive: 
    	Pepperoni pizza for the price of plain
    	$0.25 off a slice of cherry pie
    	$2.00 off a whole cherry pie
    	$10.00% off each Pi charm 
    Please choose an option:
    	1) Update Pizza Order
    	2) Update Cherry Pie Order
    	3) Update Charm Order
    	4) Check Out
    4
    Here is your subtotal:
     
    	No items purchased! Thanks anyway for stopping!
     
    Welcome to Pies, Pies and Pis
    Is there a customer in line? (1 = yes, 2 = no) > 2

  7. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: How to make program ask for options again

    Can you post the actual assignment please.

  8. #6
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again


  9. #7
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

    So I tried the switch statement, any advises?
     
    import java.util.Scanner; 
    public class asgmt1 {
     
    public static void main (String [] args) {
    int x , y, o, p, pp, cps, pico, co; // x: is there a customer in line?
    									// y: are you a Pie card member?
    									// o: input for "Please Choose an Option"
    									// p: Plain Pizza input
    									// pp: Peperoni Pizza input
    									// cps: Cherry Pie Slices input	
    									// pico: Pi Charms input
    									// co: checkout
     
    		Scanner inScan = new Scanner(System.in);
     
    System.out.println("Welcome to Pies, Pies and Pis");
    System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
    x = inScan.nextInt();
    if(x == 1){
        System.out.println("Are you a Pie card member? (1 = yes, 2 = no)");
        y = inScan.nextInt();
     
    if(y == 1){
     
    System.out.println("Welcome Back, Pie Card holder!:\n" +
    "You will receive:\n" + "        Pepperoni pizza for the price of plain\n" +
    "        $0.25 off a slice of cherry pie\n" +
    "        $2.00 off a whole cherry pie\n" + "        $10.00% off each Pi charm"); 
    }
     
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
     
    switch (o) {
    	case 1:
        System.out.println("Here is your current order:\n" +
    "        No pizzas ordered");
    System.out.println("How many plain pizzas would you like for $10.00 each?");
    p = inScan.nextInt();
    System.out.println("How many pepperoni pizzas would you like for $12.00 each?");
    pp = inScan.nextInt();
    		break;
     
    	case 2:   
    System.out.println("Here is your current order:\n" +
    "        No cherry pie slices ordered");
    System.out.println("How many cherry pie slices would you like for:\n" +
    "        $2.00 per slice\n" +"        $10.00 per pie (6 slices)");
    cps = inScan.nextInt();
    		break;
     
    	case 3:
    System.out.println("Here is your current order:\n" +
    "No gold Pi charm ordered");
    System.out.println("How many Pi charms would you like for $50.00 each?");
    pico = inScan.nextInt();
    		break;
     
    	case 4:
    System.out.println("Here is your subtotal:\n\n" +
    "       No items purchased! Thanks anyway for stopping!");
    co = inScan.nextInt();
    			break;
    	default:
    	System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
    		return;
     
    			}
    		}
    	}
    }

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make program ask for options again

    What happens using the switch statement?

    What is your problem now?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

    the problem is the whenever the program finishes one the case the I choose it doesnt loop back to this:
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
    this should keep prompting until the user selects case 4 and then the program will end

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make program ask for options again

    If you want the program to repeat a section of code, you need to put that code inside of a loop.
    Probably a while(condition) loop that will execute until the user has told the program to stop looping.

    You can exit a while loop either by setting the condition to false or by executing a break statement.


    BTW Your code is poorly formatted. Statements should be indented 3-4 spaces to show their logic nesting level. Only the first and last statements should start in the first column.
    Last edited by Norm; September 25th, 2012 at 09:02 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

    how can I make it loop after each specific case

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make program ask for options again

    The case statements are all in the switch() statement. The end of the switch statement could be a natural place to put the end of the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

     
     
     
    import java.util.Scanner; 
    public class LabAssign3 {
     
    public static void main (String [] args) {
    int x , y, o, p, pp, cps, pico, co; // x: is there a customer in line?
    									// y: are you a Pie card member?
    									// o: input for "Please Choose an Option"
    									// p: Plain Pizza input
    									// pp: Peperoni Pizza input
    									// cps: Cherry Pie Slices input	
    									// pico: Pi Charms input
    									// co: checkout
     
    		Scanner inScan = new Scanner(System.in);
     
    System.out.println("Welcome to Pies, Pies and Pis");
    System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
    x = inScan.nextInt();
    if(x == 1){
        System.out.println("Are you a Pie card member? (1 = yes, 2 = no)");
        y = inScan.nextInt();
     
    if(y == 1){
     
    System.out.println("Welcome Back, Pie Card holder!:\n" +
    "You will receive:\n" + "        Pepperoni pizza for the price of plain\n" +
    "        $0.25 off a slice of cherry pie\n" +
    "        $2.00 off a whole cherry pie\n" + "        $10.00% off each Pi charm"); 
    }
     
    }
     
    System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
     
    while(o <= 4){
     
    switch (o) {
     
    	case 1:
        System.out.println("Here is your current order:\n" +
    "        No pizzas ordered");
    System.out.println("How many plain pizzas would you like for $10.00 each?");
    p = inScan.nextInt();
    System.out.println("How many pepperoni pizzas would you like for $12.00 each?");
    pp = inScan.nextInt();
    break;
     
     
    	case 2:   
    System.out.println("Here is your current order:\n" +
    "        No cherry pie slices ordered");
    System.out.println("How many cherry pie slices would you like for:\n" +
    "        $2.00 per slice\n" +"        $10.00 per pie (6 slices)");
    cps = inScan.nextInt();
    		break;
     
    	case 3:
    System.out.println("Here is your current order:\n" +
    "No gold Pi charm ordered");
    System.out.println("How many Pi charms would you like for $50.00 each?");
    pico = inScan.nextInt();
     
    		break;
     
    	case 4:
    System.out.println("Here is your subtotal:\n\n" +
    "       No items purchased! Thanks anyway for stopping!");
    co = inScan.nextInt();
    			break;
     
    }
     
            System.out.println("Please choose an option:\n" +
    "	1) Update Pizza Order\n" + "	2) Update Cherry Pie Order\n" +
    "	3) Update Charm Order\n" + "	4) Check out");
    o = inScan.nextInt();
        }
     
    }
     
    }

    thanks!

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make program ask for options again

    The formatting of your code makes it very hard to read.

    Have you solved your problem now?
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    m7abraham (September 25th, 2012)

  18. #15
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make program ask for options again

    Yeah I used netbean's format tool to format it
    and yeah that solved one of my problems
    thanks soo much for all of you

Similar Threads

  1. Options window for game
    By Herah in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 1st, 2011, 07:34 AM
  2. Replies: 1
    Last Post: October 26th, 2011, 08:02 AM
  3. Trying to make a Car program and I can't seem to get it to compile
    By Necroshade in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2011, 06:45 PM
  4. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  5. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM