终止c#控制台应用程序

时间:2012-11-02 21:06:09

标签: c# console exit terminate

我正在尝试关闭我的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");
}

6 个答案:

答案 0 :(得分:4)

我认为您需要做的是右键单击您的项目,选择“属性”,然后(引用msdn.com):

  

在Visual Studio开发环境中设置此链接器选项

     

打开项目的“属性页”对话框。有关详情,请参阅设置   Visual C ++项目属性。

     

单击链接器文件夹。

     

单击“系统”属性页。

     

修改SubSystem属性。

对于SubSystem,选择Console。希望这可以帮助! :)

答案 1 :(得分:3)

我建议你试试application.close()函数。它已经多次挽救了我的一天。如果它没有帮助,请告诉我。

答案 2 :(得分:1)

我首先在调试中运行你的应用程序并检查以确保你的Application.Exit实际上正在受到攻击。这可能会解释应用程序未退出的原因。

您应该使用Environment.Exit(0)。这似乎是结束控制台应用程序的更好方法。

来源:http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

如果此代码在Main中我会建议使用return;在你打电话退出应用程序之后。

答案 3 :(得分:0)

如果从Visual Studio启动控制台应用程序而不是在调试模式下(Ctrl + F5),则在应用程序完成执行后,控制台窗口将保持打开。它会提示Press any key to continue...此时,您的控制台应用程序已退出。

当您运行* .exe文件时,您的控制台将关闭而不要求按键。这只是Visual Studio的“功能” - 它运行cmd.exe告诉它运行您的可执行文件然后暂停。

以下是Visual Studio在没有调试的情况下启动应用程序时所执行的操作(您可以将其复制到* .bat文件并运行它)

ECHO OFF
CLS
"Path\To\Your\ConsoleApplication.exe"
PAUSE

答案 4 :(得分:0)

使用循环,让Main正常返回。此外,我还尝试简化条件检查以及字符串比较和解析。您的错误消息表明验证范围(“介于”0和100之间),前面的if / else if逻辑实际上并未执行该验证范围。例如,如果用户输入负值,则第一种情况(...&lt; = 50)将为真。此外,我没有看到价格在哪里宣布,所以我在我的例子中做了一个常数。

static bool ExitRequired(string line)
{
    return string.Equals(line, "q", StringComparison.OrdinalIgnoreCase);
}

static void Main(string[] args)
{
    const double price = 10;
    int userInputDigit;
    double userCost;
    string line = null;

    while (!ExitRequired(line))
    {
        Console.WriteLine("Enter a number or press 'q' to exit...");
        line = Console.ReadLine();

        if (ExitRequired(line))
            break;

        if (int.TryParse(line, out userInputDigit) 
            && userInputDigit > 0 
            && userInputDigit < 100)
        {
            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");
        }
    }
}

答案 5 :(得分:0)

从Visual Studio运行项目时,将添加“按任意键继续”文本。

相反,转到垃圾箱并从那里运行。

确保在运行之前构建项目。如果从VS运行,它将根据需要自动构建,但从Windows资源管理器运行该文件不会这样做。