【发布时间】:2012-11-13 08:41:14
【问题描述】:
我正在尝试关闭我的 c# 控制台应用程序(在没有调试的情况下运行)并且没有任何工作...我已经尝试了所有具有不同退出代码的常见嫌疑人,但没有任何内容终止它 =/ 是因为该选项位于一堆嵌套的 if 语句?它可能是我想念的非常简单的东西,但它现在伤害了我的大脑,请有人帮忙!我试过了:
System.Environment.Exit(0);
System.Environment.Exit(1);
System.Environment.Exit(-1);
return;
Application.Exit(); //(wont even except it)
如果上下文有帮助,我使用嵌套 if 语句来检查用户是否输入了数字或字母“q”,如果他们输入了数字,则执行计算,如果输入了字母 q,则程序是退出并输出任何其他错误语句。
string userInput;
int userInputDigit = 0;
double userCost = 0;
char userInputChar;
userInput = Convert.ToString(Console.ReadLine());
if (int.TryParse(userInput, out userInputDigit))
{
if (userInputDigit <= 50)
{
userCost = (price * userInputDigit);
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
else if ((userInputDigit > 50) && (userInputDigit <= 80))
{
userCost = (price * 50) + ((userInputDigit - 50) * (price - 1));
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
else if ((userInputDigit > 80) && (userInputDigit <= 100))
{
userCost = (price * 50) + (30 * (price - 1)) + ((userInputDigit - 80) * (price - 2.50));
Console.WriteLine("You have purchased {0} Widgets at a cost of {1:c0}", userInputDigit, userCost);
}
else
{
Console.WriteLine("Error! Please input a number between 0 and 100");
}
}
else if (char.TryParse(userInput, out userInputChar))
{
if ((userInput == "q") || (userInput == "Q"))
{
System.Environment.Exit(0);
}
else
{
Console.WriteLine("Incorrect Letter Inputted");
}
}
else
{
Console.WriteLine("Error! Please input a number or 'q' to quit");
}
【问题讨论】:
-
你是如何运行它的?您是否在 Visual Studio 中选择“调试--> 不调试启动”?您是否手动打开控制台窗口,然后输入 .exe 名称?
-
所以您已经编写了一个控制台程序,并且您可能从命令行运行它。当执行到达主函数的末尾时,它应该自行退出。如果您注释掉 main 中的所有内容,构建程序并运行它,它应该会运行并退出。是吗?
-
在调试器中检查
System.Environment.Exit(0)是否被执行。 -
如果可能的话,最好看看代码本身。否则,您是否退出 Main 方法(通过 return;从 Main 方法中)?
-
是的,我正在使用没有调试的启动我想绕过按任意键退出我希望按下字母“q”(按回车键提交)退出它...我'不知道如何检查它是否被执行了