简体   繁体   English

当有人输入错误的答案时,C#Console关闭

[英]C# Console close when someone types the wrong answer

I've been trying to figure out how to get my console to close when someone finally types the wrong answer (in my quiz)? 当有人最终输入错误的答案时(我的测验中),我一直试图弄清楚如何让我的控制台关闭?

I've tried Environment.Exit(); 我试过Environment.Exit(); but it doesn't work... It gives me the error: "no overload for method 'Exit' takes 0 arguments. 但它不起作用......它给了我错误:“方法'没有重载'退出'需要0个参数。

Here's a bit of my code. 这是我的一些代码。 I've marked with a comment where I'd want the quit code to be: 我已经标记了我想要退出代码的注释:

Console.WriteLine("In what country was Adolf Hitler born?");

string userAnswer = Console.ReadLine();

if (Answers.AnswerOne.Equals(userAnswer))
{
    Console.WriteLine("Congratulations! {0} is the correct answer!", Answers.AnswerOne);
    Console.WriteLine("\n\n(Press Enter to move on to the next question...)");
}
else if (!Answers.AnswerOne.Equals(userAnswer))
{
    Console.WriteLine("Sorry! You wrote the wrong answer!\nThe correct answer was {0}.", Answers.AnswerOne);
    Console.WriteLine("\n\n(Press Enter to exit...)");
    Console.ReadKey();
    //EXIT CONSOLE
}

Otherwise, the code runs perfectly fine. 否则,代码运行完全正常。

Thanks in advance. 提前致谢。

The error message you are getting is 100% correct. 您获得的错误消息是100%正确的。 Environment.Exit() needs to be called with a parameter. 需要使用参数调用Environment.Exit()

Try Environment.Exit(0) . 尝试Environment.Exit(0) The .Exit() method takes an int value to indicate the 'exit code'. .Exit()方法采用int值来表示'退出代码'。

Exit code to be given to the operating system. 退出代码给操作系统。 Use 0 (zero) to indicate that the process completed successfully. 使用0(零)表示该过程已成功完成。

I think you will find multiple answers here: How do I specify the exit code of a console application in .NET? 我想你会在这里找到多个答案: 如何在.NET中指定控制台应用程序的退出代码?

Google is your friend. 谷歌是你的朋友。 :) :)

As for Environment.Exit(int exitCode): 至于Environment.Exit(int exitCode):

// Summary:
//     Terminates this process and gives the underlying operating system the specified
//     exit code.
//
// Parameters:
//   exitCode:
//     Exit code to be given to the operating system.

0 is the default exit code when nothing went wrong. 0是没有出错的默认退出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM