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 :
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;
}
}
Re: Ending Program With While Loop
Have you tried with a continue; instead of a break; (except for case 7)?
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.
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.
Code:
case 2:
if (ogre != null) {
ogre.printDetails();
mainmenu();
} else {
display("Please Create Hek!");
mainmenu();
}
break;