2012-11-02 197 views
1

我试图接近我的C#控制台应用程序(不调试运行),并没有什么工作。我已经尝试了所有不同的退出代码,但没有通常的嫌疑人被终止它= /是不是因为该选项是在一堆嵌套的if语句?它可能是一个非常简单的东西,我想念但是它伤害了我的大脑现在有人请帮助!我已经试过:终止C#控制台应用程序

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"); 
} 
+0

你好吗?您是否在Visual Studio中选择了“Debug - > Start without Debugging”?你是否手动打开控制台窗口,然后输入.exe名称? –

+1

所以你写了一个控制台程序,你大概是从命令行运行它的。当执行到达主函数的末尾时它应该自行退出。如果你注释掉main中的所有内容,构建你的程序并运行它,它应该运行并退出。可以? –

+0

在调试器中,检查“System.Environment.Exit(0)”是否被执行。 – millimoose

回答

0

使用一个循环,只是让Main正常返回。此外,我还试图简化条件检查与字符串比较和解析。您的错误消息提示验证范围(“在0和100之间”),但实际上并未由前面的if/else if逻辑强制实施。例如,如果用户输入负值,您的第一个案例(... < = 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"); 
     } 
    } 
} 
+0

谢谢!我试了一下... Price在程序中声明的更早,因为我在循环中以10为增量输出小部件的价格...我只使用了for循环,并且演示循环是分配目的,因此它可能会受益我的理解!这(做吗?)循环保持程序要求输入,直到用户输入q? –

+0

正确...如果这有帮助,请将此标记为答案或让我知道我错过了什么,我会尽力改进它。 – blins

+0

我会做,但将不会有机会,直到明天下班后,所以应该让你知道然后=] –

4

我想你需要做的是在你的项目上单击右键,选择“属性”,然后(报价msdn.com):

要在视觉设置此链接选项Studio开发环境

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

单击链接器文件夹。

单击系统属性页面。

修改SubSystem属性。

对于子系统,请选择控制台。希望这可以帮助! :)

+0

这将适用于C#应用程序? –

1

我会首先运行你的应用程序的调试和检查,以确保您的Application.Exit其实是击中。这可能会让我们了解应用程序为什么不退出。

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

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

如果这个代码是在主我会建议使用一个回报;在您的电话退出应用程序后。

+0

是的,它结束时调试,但我打算在环境中运行它并使用代码退出请求我试过environment.exit(0);但仍然显示“按任意键继续” –

+0

在调用退出应用程序之前是否有Console.ReadLine()? –

+0

或者,您是否正在使用编译时间指令(如#if debug#)在调试时执行某些操作? –

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 
+0

是的,他们是我的想法,但作业问'q'是要终止程序,我们没有被告知调试我们的程序= /谢谢大家的帮助,我将不得不给我的导师发邮件并要求她解释什么这项任务是要求=/ –

+0

啊,所以只是沟通不畅,然后我猜测什么是任务...谢谢=] –

3

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

+0

我试过= [它不喜欢“应用程序”出于某种原因(它不出现在下拉) –

0

“按任意键继续”文本当您运行从Visual Studio项目的增加。

取而代之的是,进入垃圾箱并从那里运行它。

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