Results 1 to 4 of 4
Thread: Ending Program With While Loop
- 11-28-2011, 06:23 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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; } }
- 11-28-2011, 06:35 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Re: Ending Program With While Loop
Have you tried with a continue; instead of a break; (except for case 7)?
- 11-28-2011, 06:37 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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.
- 11-28-2011, 06:40 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
-
ending a csv
By paul1024 in forum New To JavaReplies: 2Last Post: 05-04-2011, 02:14 AM -
Why is my java applet program not ending?
By Ryamza in forum New To JavaReplies: 3Last Post: 05-04-2011, 01:13 AM -
Problem with Ending Craps game
By Ebayer in forum New To JavaReplies: 4Last Post: 02-11-2011, 07:17 PM -
ending a graphics class
By imorio in forum AWT / SwingReplies: 11Last Post: 08-16-2010, 05:41 PM -
[SOLVED] Ending for loop with a String
By jdiesel in forum New To JavaReplies: 3Last Post: 01-08-2009, 02:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks