0

誰かが最終的に間違った答えを(私のクイズで)入力したときにコンソールを閉じる方法を見つけようとしていますか?

私はEnvironment.Exit();を試しました。しかし、それは機能しません...それは私にエラーを与えます:「メソッド'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
}

それ以外の場合、コードは完全に正常に実行されます。

前もって感謝します。

4

2 に答える 2

1

表示されるエラーメッセージは100%正しいです。Environment.Exit()パラメータを使用して呼び出す必要があります。

試してみてくださいEnvironment.Exit(0)。この.Exit()メソッドは、「終了コード」を示すためにint値を取ります。

オペレーティングシステムに渡される終了コード。0(ゼロ)を使用して、プロセスが正常に完了したことを示します。

于 2012-10-28T13:30:23.133 に答える
1

ここで複数の答えが見つかると思います。.NETでコンソールアプリケーションの終了コードを指定するにはどうすればよいですか。

Googleはあなたの友達です。:)

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は、何も問題がなかった場合のデフォルトの終了コードです。

于 2012-10-28T13:30:38.023 に答える