簡體   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