Results 1 to 4 of 4
  1. #1
    method22 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Ending Program With While Loop

    Hi, Been stuck with this problem a while now so thought I'd make a thread see if anyone can help me out.

    I'm trying to end my program when a user enters the number 7.
    This code works if you just load the program and then enter 7, but If I choose enter any other option before 7 it won't end.
    Anyone have any ideas as to why?
    Cheers!

    Attached the code here :
    Java Code:
    String test = JOptionPane.showInputDialog(null, options);
    		int selection = 0;
    		try {
    			selection = Integer.parseInt(test);
    		}
    
    		catch (NumberFormatException e) {
    
    			display("Please Enter A Number");
    			mainmenu();
    		}
    
    		int loop = 0;
    		while (loop != 1) {
    
    			switch (selection) {
    			case 1:
    				if (ogre == null) {
    					ogre = new Ogre("Hek");
    					display("New Hek Created");
    					mainmenu();
    
    					break;
    				} else {
    					display("Already created Hek");
    					mainmenu();
    					break;
    				}
    
    			case 2:
    				if (ogre != null) {
    					ogre.printDetails();
    					mainmenu();
    				} else {
    					display("Please Create Hek!");
    					mainmenu();
    				}
    
    				break;
    			case 3:
    				weaponmenu();
    				break;
    			case 4:
    				moodmenu();
    				break;
    			case 5:
    				foodmenu();
    				break;
    			case 6:
    				enemymenu();
    				break;
    			case 7:
    				loop = 1;
    				break;
    
    			}
    		}

  2. #2
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: Ending Program With While Loop

    Have you tried with a continue; instead of a break; (except for case 7)?

  3. #3
    method22 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Ending Program With While Loop

    Yeah, did try that still doesn't give correct output. Think it has something to do with the if statement in case 1, but can't quite see the error.

  4. #4
    method22 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Ending Program With While Loop

    Aha! Fixed my error, was simply having break; inside the if and else statements rather than outside.

    Cheers for your suggestion though.

    Java Code:
     case 2:
    				if (ogre != null) {
    					ogre.printDetails();
    					mainmenu();
    				} else {
    					display("Please Create Hek!");
    					mainmenu();
    				}
    
    				break;

Similar Threads

  1. ending a csv
    By paul1024 in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 02:14 AM
  2. Why is my java applet program not ending?
    By Ryamza in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 01:13 AM
  3. Problem with Ending Craps game
    By Ebayer in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 07:17 PM
  4. ending a graphics class
    By imorio in forum AWT / Swing
    Replies: 11
    Last Post: 08-16-2010, 05:41 PM
  5. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 02:11 AM

Posting Permissions

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