我有这个控制台应用程序,它是非常基本的,因为你可以看到并且我试图把它带到它说的地方
Console.WriteLine("你想回到主菜单吗?Y / N");
让我说我不要按Y或N ..而是让我们说F应用程序才关闭..怎么样? 我应该用什么方法来解决这个问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
class Program
{
static void Main(string[] args)
{
startMenu();
}
public static void startMenu()
{
Console.WriteLine("======= Welcome To The Player Registry =======");
Console.WriteLine("1. Name");
Console.WriteLine("2. Age");
Console.WriteLine("3. City");
try
{
int value = Convert.ToInt32(Console.ReadLine());
int answer = value;
switch (answer)
{
case 1:
Console.Clear();
Console.WriteLine("======= Player Names=======");
Console.WriteLine("Jonsson, Adam");
Console.WriteLine("Jetsson, Carl");
Console.WriteLine("Jimmy, Golf");
Console.WriteLine("Ali, Mohammed");
Console.WriteLine();
Console.WriteLine("Would you like to go back to the main menu? Y/N");
string choice = Console.ReadLine();
if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase) || choice.Equals("y", StringComparison.OrdinalIgnoreCase))
{
Console.Clear();
startMeny();
}
if (choice.Equals("N", StringComparison.OrdinalIgnoreCase) || choice.Equals("n", StringComparison.OrdinalIgnoreCase))
{
Console.Clear();
Console.WriteLine("Okay.. Bye!");
Console.ReadKey();
}
break;
case 2:
Console.WriteLine("Look it works!");
break;
default:
Console.WriteLine("What?..");
return;
}
}
catch
{
}
}
}
}
答案 0 :(得分:0)
当你点击任何其他键时它会关闭,因为应用程序已完成执行。为了强制应用程序等待,您需要添加行Console.ReadLine();
。在您的情况下,您可以将代码更改为此
if (choice.Equals("Y", StringComparison.OrdinalIgnoreCase) || choice.Equals("y", StringComparison.OrdinalIgnoreCase))
{
Console.Clear();
startMeny();
}
else if (choice.Equals("N", StringComparison.OrdinalIgnoreCase) || choice.Equals("n", StringComparison.OrdinalIgnoreCase))
{
Console.Clear();
Console.WriteLine("Okay.. Bye!");
Console.ReadKey();
}
else
{
Console.WriteLine("Wrong Key");
Console.ReadLine();
}
break;
case 2:
Console.WriteLine("Look it works!");
Console.ReadLine();
break;
default:
Console.WriteLine("What?..");
Console.ReadLine();
return;
答案 1 :(得分:0)
控制台应用程序正在关闭,因为它只会运行,直到它无需执行任何操作。避免关闭程序的一个好方法是让用户选择执行其他操作。
这些都需要更多代码来匹配您的项目,但这只是一个简单的想法:
你可以创建一些方法来帮助你:
public static string BackToMainMenu()
{
Console.WriteLine("Would you like to go back to the main menu? Y/N");
return Console.ReadLine(); ///returns the user's choice in a string variable
}
public static void CheckChoice(string choice)
{
if (choice == "y")
{
///back at menu stuff goes here
}
if (choice == "n")
{
///whatever you want to do on "n"
}
///if user types anything other than y or n (could also use 'else')
if (choice != "n" && choice != "y")
{
Console.WriteLine("Incorrect Input");
///Ask user if they want to go back to menu (again)
///check their choice
CheckChoice(BackToMainMenu());
}
}
将案例1中的内容替换为:
case 1:
Console.Clear();
Console.WriteLine("======= Player Names=======");
Console.WriteLine("Jonsson, Adam");
Console.WriteLine("Jetsson, Carl");
Console.WriteLine("Jimmy, Golf");
Console.WriteLine("Ali, Mohammed");
Console.WriteLine();
///ask "back to menu?" and check choice
CheckChoice(BackToMainMenu());