jacob_1988
Member
- Joined
- Mar 3, 2016
- Messages
- 9
- Programming Experience
- Beginner
I have a few questions regarding a Menu with C# (as below).
The first problem is how do I write a line of code that will close the Console Application when prompted, i.e. if I write the message 'Do you want to quit (y/n) ?' - and they press Y - how would I write code that wouldexi the application?
My second problem is how would I center the whole menu at once? I can center one line at a time using the code below... but I need to center the whole menu?
The first problem is how do I write a line of code that will close the Console Application when prompted, i.e. if I write the message 'Do you want to quit (y/n) ?' - and they press Y - how would I write code that wouldexi the application?
My second problem is how would I center the whole menu at once? I can center one line at a time using the code below... but I need to center the whole menu?
string textToEnter = "Menu"; Console.SetCursorPosition((Console.WindowWidth - textToEnter.Length) / 2, Console.CursorTop); Console.WriteLine("1. - New Game"); Console.Write("****************************"); Console.WriteLine(); Console.Write("Menu:"); Console.WriteLine(); Console.WriteLine("1. - New Game" + Environment.NewLine + "2. - Load Game " + Environment.NewLine + "3. - Options" + Environment.NewLine + "4. - Quit"); Console.Write("**************************** \r\n"); Console.WriteLine(); Console.Write("Enter a number from the menu "); char answer = Console.ReadLine()[0]; if (answer == '1') { Console.WriteLine("New Game will begin..."); } if (answer == '2') { Console.WriteLine("Loading Game..."); } if (answer == '3') { Console.WriteLine("Options"); } if (answer == '4') { Console.Write("Do you really want to quit the Game? (y, n): "); char YN = Console.ReadLine()[0]; if (YN == 'y' || YN == 'Y') { //char exitCode = Console.ReadLine()[0]; //Environment.Exit(0); //Console.WriteLine("Environment.Exit"); } else if (YN == 'n' || YN == 'N') { Console.WriteLine("Continue Game..."); } } else { Console.WriteLine("Enter numbers one to four only!"); } Console.WriteLine();
Last edited by a moderator: