Re: While Loop Problem :(
try this
Code :
import java.io.Console;
public class DisplayMenu {
public static int DisplayMenu() {
int choice = 0;
Console cons = System.console();
System.out
.println("\t\t\nPlease choose your option to deal with this system\n\t'1' -> To go into the while loop\n\t'-1' -> To terminate the program\n");
String line = cons.readLine();
Integer.parseInt(line);
return choice = Integer.parseInt(line);
}
public static void main(String[] args) {
DisplayMenu menu = new DisplayMenu();
int choice;
choice = menu.DisplayMenu();
while (choice != -1) {
System.out.println("\nI am inside while loop !\n");
}
System.out.println("\nI am outside while loop ! Program ends...\n");
}
}
don't start this code inside an ide but only from a os-box with java DisplayMenu
Re: While Loop Problem :(
Thanks a lot j2me64...Here i solved that problem in this way:
In the MAIN method i had written:
Code :
int choice;
choice = DisplayMenu();
while (choice != -1){
System.out.println("\nI am inside while loop !\n");
}
System.out.println("\nThanks for using this program !\n");
And in the DISPLAYMENU method i had written:
Code :
public static int DisplayMenu() // User-defined method to display a menu
{
int choice;
System.out.println(" Please select how you want to deal with the system\n '1' -> Go inside of the while loop\n '-1' -> Go outside of the while loop...Program Ends !\n");
Scanner in = new Scanner(System.in);
choice=in.nextInt();
return choice;
}
And this is running exactly like the way i want too.......