繁体   English   中英

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

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

当有人最终输入错误的答案时(我的测验中),我一直试图弄清楚如何让我的控制台关闭?

我试过Environment.Exit(); 但它不起作用......它给了我错误:“方法'没有重载'退出'需要0个参数。

这是我的一些代码。 我已经标记了我想要退出代码的注释:

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
}

否则,代码运行完全正常。

提前致谢。

您获得的错误消息是100%正确的。 需要使用参数调用Environment.Exit()

尝试Environment.Exit(0) .Exit()方法采用int值来表示'退出代码'。

退出代码给操作系统。 使用0(零)表示该过程已成功完成。

我想你会在这里找到多个答案: 如何在.NET中指定控制台应用程序的退出代码?

谷歌是你的朋友。 :)

至于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是没有出错的默认退出代码。

暂无
暂无

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

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